【问题标题】:Trying to implement python TestSuite尝试实现 python TestSuite
【发布时间】:2012-08-14 05:09:32
【问题描述】:

我有两个测试用例(两个不同的文件),我想在一个测试套件中一起运行。我可以通过“正常”运行 python 来运行测试,但是当我选择运行 python-unit 测试时,它说 0 个测试运行。现在我只是想让至少一项测试正确运行。

import usertest
import configtest # first test
import unittest   # second test

testSuite = unittest.TestSuite()
testResult = unittest.TestResult()
confTest = configtest.ConfigTestCase()
testSuite.addTest(configtest.suite())
test = testSuite.run(testResult)
print testResult.testsRun # prints 1 if run "normally"

这是我的测试用例设置示例

class ConfigTestCase(unittest.TestCase):
    def setUp(self):

        ##set up code

    def runTest(self):

        #runs test


def suite():
    """
        Gather all the tests from this module in a test suite.
    """
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(ConfigTestCase))
    return test_suite

if __name__ == "__main__":
    #So you can run tests from this module individually.
    unittest.main()

我必须做些什么才能正确完成这项工作?

【问题讨论】:

    标签: python unit-testing regression-testing test-suite python-unittest


    【解决方案1】:

    您想使用测试服。所以你不需要调用 unittest.main()。 testsuit的使用应该是这样的:

    #import usertest
    #import configtest # first test
    import unittest   # second test
    
    class ConfigTestCase(unittest.TestCase):
        def setUp(self):
            print 'stp'
            ##set up code
    
        def runTest(self):
            #runs test
            print 'stp'
    
    def suite():
        """
            Gather all the tests from this module in a test suite.
        """
        test_suite = unittest.TestSuite()
        test_suite.addTest(unittest.makeSuite(ConfigTestCase))
        return test_suite
    
    mySuit=suite()
    
    runner=unittest.TextTestRunner()
    runner.run(mySuit)
    

    【讨论】:

    • 谢谢你,谢谢完美地满足了我的需要。非常感谢!
    • 非常感谢您的回答。为什么我需要致电unittest.makeSuite 才能向现有 套件添加测试?
    • 在 cmets 中是 GatherGathers?
    • @OlivierPons 它的集合。建议测试人员
    • 好吧,我想“在一个测试套件中收集来自这个模块的所有测试。”意思是“这个函数在一个测试套件中收集这个模块的所有测试。” (这就是为什么我认为应该添加一个s,但我的英语很糟糕,抱歉)
    【解决方案2】:

    创建加载器和套件的所有代码都是不必要的。您应该编写测试,以便使用您最喜欢的测试运行器通过测试发现运行它们。这只是意味着以标准方式命名您的方法,将它们放在可导入的位置(或将包含它们的文件夹传递给运行器),并从 unittest.TestCase 继承。完成后,您可以最简单地使用python -m unittest discover,或者使用更好的第三方运行程序来发现并运行您的测试。

    【讨论】:

      【解决方案3】:

      如果您尝试手动收集TestCases,这很有用:unittest.loader.findTestCases()

      # Given a module, M, with tests:
      mySuite = unittest.loader.findTestCases(M)
      runner = unittest.TextTestRunner()
      runner.run(mySuit)
      

      【讨论】:

        【解决方案4】:

        我假设您指的是针对合并这两个测试的模块运行 python-unit 测试。如果您为该模块创建测试用例,它将起作用。子类化 unittest.TestCase 并进行一个以单词“test”开头的简单测试。

        例如

        class testall(unittest.TestCase):
        
            def test_all(self):           
                testSuite = unittest.TestSuite()
                testResult = unittest.TestResult()
                confTest = configtest.ConfigTestCase()
                testSuite.addTest(configtest.suite())
                test = testSuite.run(testResult)
                print testResult.testsRun # prints 1 if run "normally"
        
        if __name__ == "__main__": 
              unittest.main()
        

        【讨论】:

          猜你喜欢
          • 2011-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-16
          • 1970-01-01
          • 2010-11-05
          • 2018-07-22
          相关资源
          最近更新 更多