你好,我有使用单元测试的测试。我有一个测试套件,我正在尝试将变量传递到每个测试中。下面的代码显示了使用的测试套件。
class suite():
def suite(self): #Function stores all the modules to be tested
modules_to_test = ('testmodule1', 'testmodule2')
alltests = unittest.TestSuite()
for module in map(__import__, modules_to_test):
alltests.addTest(unittest.findTestCases(module))
return alltests
它调用测试,我想知道如何将变量传递到此类的测试中。示例测试脚本如下:
class TestThis(unittest.TestCase):
def runTest(self):
assertEqual('1', '1')
class TestThisTestSuite(unittest.TestSuite):
# Tests to be tested by test suite
def makeTestThisTestSuite():
suite = unittest.TestSuite()
suite.addTest("TestThis")
return suite
def suite():
return unittest.makeSuite(TestThis)
if __name__ == '__main__':
unittest.main()
所以从 class suite()
我想输入一个值来更改断言值中的值。 例如。 assertEqual(self.value, '1')
.我已经为 unittest 尝试了 sys.argv,但它似乎不起作用。
感谢您的帮助。
最佳答案
在命令行传递交互值并不真正符合自动化单元测试的意图。如何使用 ConfigParser 库模块并让您的 TestCase
子类的 __init__
方法以这种方式加载一些可变测试输入数据?
当然,根据您的代码,模块全局变量或类常量成员如何保存您的测试数据?例如,在按名称导入测试模块后,你不能直接做吗
module.testVars = [1, 2, 3, "foo"]
并让测试模块中的测试引用该变量(默认情况下可能是一个空列表)?
关于python - 如何使用 Unittest 套件传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2812132/