我试图从另一个python文件调用unittest,并评估退出代码。 我能够使用unittest.TestLoader().loadTestsFromModule和unittest.TextTestRunner.run从另一个python文件中调用unittest,但是这unittest.TestLoader().loadTestsFromModule整个结果返回给cmd。 我想简单地设置一个变量等于状态码,以便我可以评估它。 我能够找到一个方法unittest.TestResult.wasSuccessful,但我遇到了麻烦实施它。 当我将它添加到用例中时,我得到以下AttributeError: AttributeError: 'ConnectionTest' object has no attribute 'failures'
我已经在下面列出了一些代码示例,并将所需结果的模型作为我试图实现的一个示例。 先谢谢你。
""" Tests/ConnectionTest.py """ import unittest from Connection import Connection class ConnectionTest(unittest.TestCase): def test_connection(self): #my tests def test_pass(self): return unittest.TestResult.wasSuccessful(self) if __name__ == '__main__': unittest.main() """ StatusTest.py """ import unittest import Tests.ConnectionTest as test #import Tests.Test2 as test2 #import Tests.Test3 as test3 #import other unit tests ... suite = unittest.TestLoader().loadTestsFromModule(test) unittest.TextTestRunner(verbosity=2).run(suite) """ Return True if unit test passed """ def test_passed(test): if test.test_pass() == 0: return True else: return False """ Run unittest for each module before using it in code """ def main(): tests = "test test2 test3".split() for test in tests: if test_passed(test): # do something else: # log failure pass更新
为了更简单地提出问题,我需要将突出显示的变量设置为突出显示的值。
I'm trying to call a unittest from another python file, and evaluate the exit code. I was able to use unittest.TestLoader().loadTestsFromModule and unittest.TextTestRunner.run to call the unittest from another python file, but that's returning the entire results to the cmd. I would like to simply set a variable equal to the status code so I can evaluate it. I was able to find a method unittest.TestResult.wasSuccessful, but I'm having trouble implementing it. When I add it to the use case, I get the following AttributeError: AttributeError: 'ConnectionTest' object has no attribute 'failures'
I've included some code samples below and a mockup of the desired result as an illustration of what I'm trying to achieve. Thank you in advance.
""" Tests/ConnectionTest.py """ import unittest from Connection import Connection class ConnectionTest(unittest.TestCase): def test_connection(self): #my tests def test_pass(self): return unittest.TestResult.wasSuccessful(self) if __name__ == '__main__': unittest.main() """ StatusTest.py """ import unittest import Tests.ConnectionTest as test #import Tests.Test2 as test2 #import Tests.Test3 as test3 #import other unit tests ... suite = unittest.TestLoader().loadTestsFromModule(test) unittest.TextTestRunner(verbosity=2).run(suite) """ Return True if unit test passed """ def test_passed(test): if test.test_pass() == 0: return True else: return False """ Run unittest for each module before using it in code """ def main(): tests = "test test2 test3".split() for test in tests: if test_passed(test): # do something else: # log failure passUpdate
To put the question more simply, I need to set the highlighted variable below to the highlighted value.
最满意答案
你提到你尝试实现result.wasSuccessful ,但会像下面的工作:
result = unittest.TextTestRunner(verbosity=2).run(suite) test_exit_code = int(not result.wasSuccessful())test_exit_code的值将在测试套件成功运行时为0,否则为1。
如果你想禁用TextTestRunner的输出,你可以指定你自己的流,比如:
from io import StringIO result = unittest.TextTestRunner(stream=StringIO(), verbosity=2).run(suite)You mentioned you tried implementing result.wasSuccessful, but would something like the following work:
result = unittest.TextTestRunner(verbosity=2).run(suite) test_exit_code = int(not result.wasSuccessful())The value of test_exit_code would then be either 0 when the test suite ran successfully or 1 otherwise.
If you want to disable the output of the TextTestRunner you can specify your own stream, such as:
from io import StringIO result = unittest.TextTestRunner(stream=StringIO(), verbosity=2).run(suite)更多推荐
发布评论