gpt4 book ai didi

python - PyUnit 中是否弃用了测试套件?

转载 作者:行者123 更新时间:2023-11-28 20:53:59 26 4
gpt4 key购买 nike

按照 PyUnit 中的示例,我想出了以下运行良好的单元测试代码。

import unittest

class Board:
def __init__(self, x, y):
self.x = x; self.y = y;
def __eq__(self, other):
return self.x == other.x and self.y == other.y

class BoardTest(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))

class BoardTest2(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))

def suite():
suite1 = unittest.makeSuite(BoardTest)
suite2 = unittest.makeSuite(BoardTest2)
return unittest.TestSuite((suite1, suite2))

if __name__ == "__main__":
unittest.main()

但问题是,即使我删除了

def suite():
, 结果是一样的。换句话说,看起来 fixture/suite 对 PyUnit 来说并非毫无用处。

这是正确的吗?

最佳答案

如果你想在单个模块中运行所有测试,则 unittest.TestSuite 不是必需的,因为 unittest.main() 将动态检查调用它的模块并找到从 unittest.TestCase 派生的所有类

但是,TestSuite 类在许多情况下仍然很方便:

  1. 您想构建一组逻辑测试分组。例如,一套单元测试、集成测试、针对特定子系统的测试等。
  2. 您的测试跨越多个模块/包。在这种情况下,拥有一个可以运行的脚本来执行所有测试是很有用的。这可以通过构建一套包含所有测试的方法来实现。请注意,这与 discovery 等库无关。 .

关于python - PyUnit 中是否弃用了测试套件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3121107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com