如何在Django测试函数中添加/更改变量
这个问题可能听起来有点傻,但我找不到答案。
我想在一个测试类中添加一个测试函数,用来检查测试是否完成。比如说,测试了网址,测试了表单等等。因此,我想有一个变量来记录每个测试的情况。如果网址测试通过了,我就想让 VARIABLE["urls"] = True。
可惜的是,似乎每个测试函数中的所有变量都会被重置。在网址测试中记录的 VARIABLE["urls"] 的信息不能在其他测试中继续使用。有没有办法让一个全局变量在所有测试函数之间共享呢?
以下是修改后的可运行代码
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
期望的结果应该是:
Warning: forms test is missing!
Warning: templates test is missing!
1 个回答
5
那类级别的属性怎么样呢?
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()