PyUnit 中是否已弃用测试套件?

发布于 2024-09-07 06:17:13 字数 1295 浏览 2 评论 0原文

按照 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():
, the result is the same. In other words, it looks like that the fixture/suite is not useless with PyUnit.

这是正确的吗?

Following the example in PyUnit, I came up with the following unittest code that works fine.

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()

But the thing is that even if I remove the

def suite():

, the result is the same. In other words, it looks like that the fixture/suite is not useless with PyUnit.

Is this correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2024-09-14 06:17:13

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

但是,TestSuite类仍然是在许多场景中都很方便:

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

unittest.TestSuite is not necessary if you want to run all the tests in a single module as unittest.main() will dynamically examine the module it is called from and find all classes that derive from unittest.TestCase

However, the TestSuite class is still handy in a number of scenarios:

  1. You want to build a set of logical groupings of tests. For instance, a suite of unit tests, integration tests, tests for a specific subsystem, etc.
  2. You tests span multiple modules/packages. In this scenario, it is useful to have a single script you can run execute all your tests. This can be accomplished by building up a suite of all your tests. Note that this becomes irrelevant with libraries such as discovery.
魔法少女 2024-09-14 06:17:13

除了马克的回答之外,构建自己的 suite() 的另一个原因是如果您正在动态构建测试。

另外,我花了一段时间才弄清楚如何让 PyDev 选择该套件并在图形测试运行器中运行它。诀窍是放入这样的方法:

def load_tests(loader, tests, pattern):
    return suite()

这样的方法会被图形测试运行器拾取。

In addition to Mark's answer, one more reason to build your own suite() is if you are dynamically building tests.

Also, it took me a while to figure out how to get PyDev to pick up the suite and run it in the graphical test runner. The trick is to put in a method like so:

def load_tests(loader, tests, pattern):
    return suite()

Such a method gets picked up the graphical test runner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文