在让你们感到困惑之前,让我澄清一下:我不是在询问运行具有不同参数的单一测试方法。全清?那我们开始吧:
我在 Python(Django,但不相关)中进行了测试,基本上...
- 启动一个http服务器,
- 启动 Selenium,在此服务器上打开网页,
- 通过 Selenium 加载并运行一套 JavaScript 测试(通过 Jasmine)
- 收集结果并在任何测试失败时失败
我想让每个 Jasmine 规范的输出在 Python 单元测试输出中作为单独的条目可见(有自己的名称)?通过 Selenium 从 Javascript 中提取它是简单的部分,但我不知道如何将它与 UnitTest 机器连接。
预期的代码看起来像(伪代码):
class FooPageTest(TestCase):
def setUp(self):
# start selenium, etc
def run(self, result):
self.run_tests()
for test_name, status, failure_message in self.get_test_results():
if status:
result.add_successful_test(test_name)
else:
result.add_failed_test(test_name, failure_message)
预期输出:
$ python manage.py test FooPageTest -v2
first_external_test ... ok
second_external_test ... ok
third_external_test ... ok
注意事项:测试用例的数量和名称只有在实际运行测试后才能知道。
是否有可能让 unittest2
屈服于我的意志?怎么办?
最佳答案
听起来您有多个外部测试要运行,并且您希望通过 Python 单元测试单独报告每个测试的结果。我想我会做类似的事情:
class FooPageTest(TestCase):
@classmethod
def setUpClass(cls):
# start selenium, etc
cls.run_tests()
@classmethod
def getATest(cls, test_name):
def getOneResult(self):
# read the result for "test_name" from the selenium results
if not status:
raise AssertionError("Test %s failed: %s" % (test_name, failure_message)
setattr(cls, 'test%s' test_name, getOneResult)
for test_name in get_test_names():
FooPageTest.getATest(test_name)
这种方法做了一些我认为不错的事情:
- 它在测试发现运行测试时运行测试,而不是在模块导入时运行测试
- 每个 selenium 测试都会生成一个 Python 测试。
要使用它,您需要定义 get_test_names(),它读取将要运行的测试的名称。您还需要一个函数来从 selenium 结果中读取每个单独的结果,但听起来您必须已经有办法做到这一点(您的 get_test_results() 方法)。
关于python - 让一个测试方法返回多个测试结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23610606/