Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中自动向测试套件添加十几个测试用例_Python_Unit Testing_Testcase - Fatal编程技术网

如何在python中自动向测试套件添加十几个测试用例

如何在python中自动向测试套件添加十几个测试用例,python,unit-testing,testcase,Python,Unit Testing,Testcase,我在不同的文件夹中有十几个测试用例。在根目录中有一个测试运行程序 unittest\ package1\ test1.py test2.py package2\ test3.py test4.py testrunner.py 目前,我将这四个测试用例手动添加到一个测试套件中 import unittest from package1.test1 import Test1 from package1.test2 import Test2 from pa

我在不同的文件夹中有十几个测试用例。在根目录中有一个测试运行程序

unittest\
  package1\
    test1.py
    test2.py
  package2\
    test3.py
    test4.py
  testrunner.py
目前,我将这四个测试用例手动添加到一个测试套件中

import unittest
from package1.test1 import Test1
from package1.test2 import Test2
from package2.test3 import Test3
from package2.test4 import Test4

suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(Test1))
suite.addTests(unittest.makeSuite(Test2))
suite.addTests(unittest.makeSuite(Test3))
suite.addTests(unittest.makeSuite(Test4))

result = unittest.TextTestRunner(verbosity=2).run(suite)
if not result.wasSuccessful():
  sys.exit(1)
如何让测试运行程序自动测试所有测试用例?例如:

for testCase in findTestCases():
  suite.addTests(testCase)

在我看来,您应该切换到或其他具有发现功能的测试框架。发现测试是运行它们的一种非常明智的方法

最著名的是:

例如,使用nosetest就足以从项目根目录运行
nosetests
,它将发现并运行它找到的所有单元测试。很简单


还要注意的是,unittest2将包含在python 2.7中(我想是后端口到2.4中)。

上面的模块很好,但是当尝试输入参数时,NoseTests可能会很有趣,这也更快,并且可以很好地将它安装到另一个模块中

import os, unittest

class Tests():   

    def suite(self): #Function stores all the modules to be tested


        modules_to_test = []
        test_dir = os.listdir('.')
        for test in test_dir:
            if test.startswith('test') and test.endswith('.py'):
                modules_to_test.append(test.rstrip('.py'))

        alltests = unittest.TestSuite()
        for module in map(__import__, modules_to_test):
            module.testvars = ["variables you want to pass through"]
            alltests.addTest(unittest.findTestCases(module))
        return alltests

if __name__ == '__main__':
    MyTests = Tests()
    unittest.main(defaultTest='MyTests.suite')
如果要将结果添加到日志文件,请在末尾添加以下内容:

if __name__ == '__main__':
    MyTests = Tests()
    log_file = 'log_file.txt'
    f = open(log_file, "w") 
    runner = unittest.TextTestRunner(f)
    unittest.main(defaultTest='MyTests.suite', testRunner=runner)
同样,在您测试的模块底部,放置如下代码:

class SomeTestSuite(unittest.TestSuite):

    # Tests to be tested by test suite
    def makeRemoveAudioSource():
        suite = unittest.TestSuite()
        suite.AddTest(TestSomething("TestSomeClass"))

        return suite

    def suite():
        return unittest.makeSuite(TestSomething)

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

我所做的是运行单独测试文件的包装器脚本:

主包装:

然后是另一个用于测试的包装器:

class FooTest(unittest.TestCase):
    def __init__(self, *args, cargs=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.vdisplay = Xvfb(width=1280, height=720)
        self.vdisplay.start()
        self.args=cargs
        self.log=logging

    def setUp(self):
        self.site = webdriver.Firefox()

    def kill(self):
        self.vdisplay.stop()
然后,单独文件中的每个测试将如下所示:

import sys, os, unittest
from FooTest import FooTest

class FooTest1(FooTest):

    def test_homepage(self):
        self.site.get(self.base_url + "/")
        log.debug("Home page loaded.")
然后,您可以轻松地从shell运行测试,如:

$ ./run_tests.py -h http://example.com/ test1.py test2.py

您可以使用通配符指定特定目录中的所有文件,或者使用(
**
)递归运行所有测试(通过
shopt-s globstar
启用)。

unittest2库听起来是一个非常有趣的工具。谢谢你提供的信息。
$ ./run_tests.py -h http://example.com/ test1.py test2.py