为什么 Python unittest 文档自相矛盾?

Why does the Python unittest doc contradict itself?

Python unittest doc 将测试用例定义为:

"[...] the smallest unit of testing. It checks for a specific response to a particular set of inputs."

但是 first example 包含一个带有两个断言的方法:

def test_shuffle(self):
    ...
    self.assertEqual(self.seq, range(10))
    ...
    self.assertRaises(TypeError, random.shuffle, (1,2,3))

这显然是矛盾的,因为每个断言都包含自己的输入和预期的响应。

哪种方法最合适?

有两个断言本身并不是问题。但是文档中的第一个例子确实测试了两个独立的东西,应该分成更小的部分。

如何精细地划分测试的问题可以成为一个哲学问题。找到适合您的平衡点。