Python unittest动态加载测试数据

Python unittest动态加载测试数据,python,testing,Python,Testing,我正试图从配置文件中加载应用程序名称上的测试数据库。 我使用的是ConfigParser,它是一个nosetest插件。 这是我的密码。我只是不知道在动态加载测试时如何通过应用程序名。我尝试了构造函数,但无法找到将参数传递给loadTestsFromTestCase方法的方法。 有什么想法吗 import unittest class TestMyApp(unittest.TestCase): def test1(self): print "test1: %s" % s

我正试图从配置文件中加载应用程序名称上的测试数据库。 我使用的是ConfigParser,它是一个nosetest插件。 这是我的密码。我只是不知道在动态加载测试时如何通过应用程序名。我尝试了构造函数,但无法找到将参数传递给loadTestsFromTestCase方法的方法。 有什么想法吗

import unittest

class TestMyApp(unittest.TestCase):
    def test1(self):
        print "test1: %s" % self.app
    def test2(self):
        print "test2: %s" % self.app

if __name__ == '__main__':
    # here specify the app name
    suite = unittest.TestLoader().loadTestsFromTestCase(TestMyApp)
    # here specify the other app name
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestMyApp))
    unittest.TextTestRunner(verbosity=2).run(suite)

你需要一个测试参数化!这很容易做到。请看以下代码示例:

导入pytest
类AppImpl:
定义初始化(自身、应用程序名称):
self.name=应用程序名称
def get_名称(自身):
返回self.name
@fixture(参数=['App1','App2'])
def应用程序(请求):
返回AppImpl(request.param)
def测试_1(应用程序):
断言app.get_name()
def测试_2(应用程序):
在App.get_name()中断言“App”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main(args=[\uuuuu文件\uuuuu,'-v'])
通过使用逻辑
AppImpl
的类实现,您可以创建一个fixture
app
,该fixture可以由特定的arg
params=['App1',App2']
参数化。然后在测试
test_1
test_2
中,在函数中使用fixture name
app
。这种可能性提供了更多的模块性和可读性测试。

这是一个有效的代码(必须使用python运行,而不是nosetests),所以我接受它。另一种选择是使用nose_参数化插件。