Вопрос

This question might be silly, but I did not find an answer.

I want to add a test function to a class of TestCase to check the completion of the test. For example, the urls were tested, the forms were tested et.al. As such, I would like to have a variable to keep the record of each test. If urls were tested, then VARIABLE["urls"] = True.

Unfortunately, it looks like all the variable were reset in each test function. The message recorded in urls test VARIABLE["urls"] can not been carried on to one other test. Is there any way to have a global variable across all test functions?

Here are the revised working code

class Test(TestCase):
    test = {}
    to_be_test = ["urls","ajax","forms","templates"]

    def test_urls(self):
        ...
        self.test['urls'] = True

    def test_ajax(self):
        ...
        self.test['ajax'] = True

    def test_z_completion(self):
        for t in self.to_be_test:
            if not t in self.test:
                print "Warning: %s test is missing!" % t

The expected result should be:

Warning: forms test is missing!
Warning: templates test is missing!
Это было полезно?

Решение

How about a class level attribute?

import unittest

class FakeTest(unittest.TestCase):
    cl_att = []

    def test_a1(self):

        self.assert_(True)
        self.cl_att.append('a1')
        print "cl_att:", self.cl_att

    def test_a2(self):

        self.assert_(True)
        self.cl_att.append('a2')
        print "cl_att:", self.cl_att


if __name__ == "__main__":
    unittest.main()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top