You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在unittest中正确编写Vector类叉乘与哈希方法单元测试

三维Vector类单元测试修复方案

现有代码说明

你已实现三维向量Vector类,包含初始化、相等判定、加减运算、点乘、叉乘、模长计算、哈希计算方法,类实现代码如下:

from math import sqrt

class Vector:

  def __init__(self, x, y, z):   
    self.x = x
    self.y = y
    self.z = z
    
  def __eq__(self, other):   # v == w
        return self.x == other.x and self.y == other.y and self.z == other.z

  def __ne__(self, other):        # v != w
        return not self == other    

  def __repr__(self): 
    return "Vector(" + str(self.x) + ", " + str(self.y) + ", " + str(self.z) + ")"

  def __add__(self, other):
    return Vector(self.x + other.x,self.y + other.y,self.z + other.z)

  def __sub__(self, other):
    return Vector(self.x - other.x, self.y - other.y, self.z - other.z)

  def __mul__(self, other):  
    return self.x * other.x + self.y * other.y + self.z * other.z
  
  def cross(self, other):
    return Vector(self.y * other.z - self.z * other.y,self.z * other.x - self.x * other.z,self.x * other.y - self.y * other.x)

  def length(self):
    return sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)

  def __hash__(self): 
    return hash((self.x, self.y, self.z))

原有测试代码存在四个核心问题:

  • 所有测试用例均使用Python原生列表运算,未实例化Vector类,完全没有覆盖自定义类的逻辑
  • 叉乘测试逻辑完全不符合三维叉乘计算规则,预期值计算错误
  • 缺少__hash__方法的测试用例
  • 点乘、模长计算的测试逻辑与类实现逻辑不匹配:__mul__实现的是点乘(返回标量),length返回的是向量欧几里得模长,原有测试逻辑均不符合实现

原有存在问题的测试代码如下:

import unittest

class TestVector(unittest.TestCase):
    
    def test_vector_equality(self):
        A = [1, 2, 3]
        B = [1, 2, 3]
        self.assertTrue(A == B)
        
    def test_vector_inequality(self):
        A = [1, 2, 3]
        B = [1,-2,-3]
        self.assertFalse(A == B)  
    
    def test_vector_addition(self):
        A = [1, 2, 3]
        B = [1, 2, 3]
        result = [2, 4, 6]
        self.assertEqual([x + y for x, y in zip(A, B)], result) 

    def test_vector_mulitplication(self):
        A = [1, 2, 3]
        B = [1, 2, 3]
        result = [1, 4, 9]
        self.assertEqual([x*y for x, y in zip(A, B)], result)
    
    def test_vector_subtraction(self):
        A = [1, 2, 3]
        B = [1, 5, 8]
        result = [0, -3, -5]
        self.assertEqual([x - y for x, y in zip(A, B)], result) 
        
    def test_vector_cross_multiplication(self):
        A = [1, 2, 3]
        B = [1, 5, 8]
        result = [1 ,5, 3]
        self.assertEqual([(x[0]*y[1], x[1]*y[0]) for x, y in zip(A, B)], result)
        
    def test_length(self):
        A = [1, 2, 3]
        B = [1, 4, 9] 
        self.assertEqual(B, [i ** 2 for i in A])          
                                                 
if __name__ == "__main__":
    unittest.main(argv=['first-arg-is-ignored'], exit= False) 

修复后完整测试代码

修复说明

  • 所有测试用例均基于Vector实例编写,真正覆盖类方法逻辑
  • 按照三维叉乘公式修正叉乘测试:若向量A=(x1,y1,z1),向量B=(x2,y2,z2),叉乘结果为Vector(y1*z2 - z1*y2, z1*x2 - x1*z2, x1*y2 - y1*x2)
  • 补充__hash__测试:核心校验相等对象哈希值必须一致,且向量实例可正常作为集合元素、字典键使用(哈希类型的核心使用场景)
  • 修正点乘、模长测试的逻辑,匹配类的实际实现
import unittest
from math import sqrt
# 若Vector类定义在其他文件,在此处添加对应导入语句

class TestVector(unittest.TestCase):
    
    def test_vector_equality(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(1, 2, 3)
        self.assertEqual(v1, v2)
        
    def test_vector_inequality(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(1, -2, -3)
        self.assertNotEqual(v1, v2)  
    
    def test_vector_addition(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(1, 2, 3)
        expect = Vector(2, 4, 6)
        self.assertEqual(v1 + v2, expect) 

    def test_vector_dot_multiplication(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(1, 2, 3)
        expect = 14 # 点乘结果为1*1 + 2*2 +3*3
        self.assertEqual(v1 * v2, expect)
    
    def test_vector_subtraction(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(1, 5, 8)
        expect = Vector(0, -3, -5)
        self.assertEqual(v1 - v2, expect) 
        
    def test_vector_cross_multiplication(self):
        v1 = Vector(1, 2, 3)
        v2 = Vector(1, 5, 8)
        # 手动计算叉乘结果:x=2*8-3*5=1, y=3*1-1*8=-5, z=1*5-2*1=3
        expect = Vector(1, -5, 3)
        self.assertEqual(v1.cross(v2), expect)
        
    def test_length(self):
        v = Vector(1, 2, 3)
        expect = sqrt(1**2 + 2**2 + 3**2)
        self.assertEqual(v.length(), expect)

    def test_vector_hash(self):
        # 校验相等向量哈希值一致,不等向量哈希值不同
        v1 = Vector(1, 2, 3)
        v2 = Vector(1, 2, 3)
        v3 = Vector(1, -2, -3)
        self.assertEqual(hash(v1), hash(v2))
        self.assertNotEqual(hash(v1), hash(v3))
        # 校验向量可正常存入集合、作为字典键,符合哈希类型使用要求
        s = {v1, v2, v3}
        self.assertEqual(len(s), 2) # v1和v2相等,集合会自动去重
        d = {v1: "test_value"}
        self.assertEqual(d[v2], "test_value")
                                                 
if __name__ == "__main__":
    unittest.main(argv=['first-arg-is-ignored'], exit= False) 

内容的提问来源于stack exchange,提问作者12666727b9

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 05:54:36