2

Before I get you all confused, let me clarify: I'm NOT asking about running a single test method with different arguments. All clear? Then let's go:

I have a test in Python (Django, but not relevant) that basically...

  • starts a http server,
  • starts Selenium, opens a web page on this server,
  • via Selenium loads and runs a suite of JavaScript tests (via Jasmine)
  • collects the results and fails if any test failed

I'd like to make the output of the each Jasmine spec visible as a separate entry in Python unit test output (with its own name)? Extracting it from Javascript via Selenium is the easy part, but I don't know how to connect it with the UnitTest machinery.

Expected code would look something like (pseudocode):

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)

Expected output:

$ python manage.py test FooPageTest -v2
first_external_test ... ok
second_external_test ... ok
third_external_test ... ok

A gotcha: The number and names of test cases would be only known after actually running the tests.

Is it possible to bend unittest2 to my will? How?

4

1 回答 1

1

听起来您有多个外部测试要运行,并且您希望通过 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(),它读取将要运行的测试的名称。您还需要一个函数来从硒结果中读取每个单独的结果,但听起来您必须已经有办法做到这一点(您的 get_test_results() 方法)。

于 2014-07-29T21:34:54.490 回答