如何在 django (unittest) 中存储许多测试用例的结果(到一个文件)?
How to store results from many TestCases (to one file) in django (unittest)?
我有几个测试用例,它们都有类似的拆解:
def tearDown(self):
execution_time = time.time() - self.startTime
result_list = [self._testMethodName]
result = [str(x) for x in sys.exc_info()]
if result[0] == 'None':
result_list.append('PASS')
elif 'Assertion' in result[0]:
result_list.append('FAIL')
else:
result_list.append('ERROR')
result_list.append(result)
result_list.append(str(execution_time))
TEST_RESULTS.append(result_list)
我正在使用 tearDown 函数将每个测试(在测试用例中)的结果存储到全局 TEST_RESULTS 对象(因此每个 TestCase 文件都有一个 TEST_RESULTS 全局定义)。
然后在 tearDownClass 函数中,我这样做是为了将结果存储到 csv:
@classmethod
def tearDownClass(cls):
with open ('tests/results/test_case_1_output.csv', 'wr') as resultFile:
wr = csv.writer(resultFile)
wr.writerows(TEST_RESULTS)
对我来说这是一个糟糕的实现。全局定义无处不在,tearDown/tearDownClass 在每个测试用例中一遍又一遍地实现,而不是定义一次。
此外,我想创建一个测试结果文件来收集所有测试用例的结果。
我的直觉是这需要在运行器级别(或调用测试用例之前的某处)定义文件句柄。这将允许我在更高级别重新初始化 csv 文件(而不是在一个 TestCase 中任意初始化)。
有人对如何实现这一目标有任何建议吗?没有从文档中看到执行此操作的方法,覆盖 django TestCase 似乎很危险。
我将 post 我的解决方案(非常感谢@xyres),因为我认为它可能对其他人有所帮助。
下面是一个 TestCase 的例子,它从基础 class(TestManager 或 TestCase)调用 SetUp、tearDown 和 setUpClass。诀窍是从基础 class [=15= 调用 setUpClass ] 并创建另一个在 'TestManager' 基 class.
上调用的初始化函数 'initialize'
class MyTestCase(TestManager, TestCase)
def setUp(self):
self.param1, self.param2 = super(MyTestCase, self).setUp()
def tearDown(self):
test_name = self._testMethodName
super(MyTestCase, self).get_and_write_results_to_csv(test_name)
@classmethod
def setUpClass(cls):
super(MyTestCase, cls).setUpClass()
super(MyTestCase, cls).initialize('my test case name')
class TestManager():
@classmethod
def initialize(cls, test_case_name):
with open('path/to/my/testresults.csv', 'wr') as resultFile:
wr = csv.writer(resultFile)
wr.writerow("Results for " + test_case_name + "are below:")
def setUp(self):
"""
Do some setup stuff that's the same for each TestCase.
Im not showing the actions here but assume you want this
function to return 2 params that are the same for every
TestCase setup
"""
return param1, param2
def get_and_write_results_to_csv(self, test_name):
execution_time = time.time() - self.startTime
result_list = [test_name]
result = [str(x) for x in sys.exc_info()]
if result[0] == 'None':
result_list.append('PASS')
elif 'Assertion' in result[0]:
result_list.append('FAIL')
else:
result_list.append('ERROR')
result_list.append(result)
result_list.append(str(execution_time))
with open('path/to/my/testresults.csv', 'a') as resultFile:
wr = csv.writer(resultFile)
wr.writerow(result_list)
我有几个测试用例,它们都有类似的拆解:
def tearDown(self):
execution_time = time.time() - self.startTime
result_list = [self._testMethodName]
result = [str(x) for x in sys.exc_info()]
if result[0] == 'None':
result_list.append('PASS')
elif 'Assertion' in result[0]:
result_list.append('FAIL')
else:
result_list.append('ERROR')
result_list.append(result)
result_list.append(str(execution_time))
TEST_RESULTS.append(result_list)
我正在使用 tearDown 函数将每个测试(在测试用例中)的结果存储到全局 TEST_RESULTS 对象(因此每个 TestCase 文件都有一个 TEST_RESULTS 全局定义)。
然后在 tearDownClass 函数中,我这样做是为了将结果存储到 csv:
@classmethod
def tearDownClass(cls):
with open ('tests/results/test_case_1_output.csv', 'wr') as resultFile:
wr = csv.writer(resultFile)
wr.writerows(TEST_RESULTS)
对我来说这是一个糟糕的实现。全局定义无处不在,tearDown/tearDownClass 在每个测试用例中一遍又一遍地实现,而不是定义一次。
此外,我想创建一个测试结果文件来收集所有测试用例的结果。
我的直觉是这需要在运行器级别(或调用测试用例之前的某处)定义文件句柄。这将允许我在更高级别重新初始化 csv 文件(而不是在一个 TestCase 中任意初始化)。
有人对如何实现这一目标有任何建议吗?没有从文档中看到执行此操作的方法,覆盖 django TestCase 似乎很危险。
我将 post 我的解决方案(非常感谢@xyres),因为我认为它可能对其他人有所帮助。
下面是一个 TestCase 的例子,它从基础 class(TestManager 或 TestCase)调用 SetUp、tearDown 和 setUpClass。诀窍是从基础 class [=15= 调用 setUpClass ] 并创建另一个在 'TestManager' 基 class.
上调用的初始化函数 'initialize'class MyTestCase(TestManager, TestCase)
def setUp(self):
self.param1, self.param2 = super(MyTestCase, self).setUp()
def tearDown(self):
test_name = self._testMethodName
super(MyTestCase, self).get_and_write_results_to_csv(test_name)
@classmethod
def setUpClass(cls):
super(MyTestCase, cls).setUpClass()
super(MyTestCase, cls).initialize('my test case name')
class TestManager():
@classmethod
def initialize(cls, test_case_name):
with open('path/to/my/testresults.csv', 'wr') as resultFile:
wr = csv.writer(resultFile)
wr.writerow("Results for " + test_case_name + "are below:")
def setUp(self):
"""
Do some setup stuff that's the same for each TestCase.
Im not showing the actions here but assume you want this
function to return 2 params that are the same for every
TestCase setup
"""
return param1, param2
def get_and_write_results_to_csv(self, test_name):
execution_time = time.time() - self.startTime
result_list = [test_name]
result = [str(x) for x in sys.exc_info()]
if result[0] == 'None':
result_list.append('PASS')
elif 'Assertion' in result[0]:
result_list.append('FAIL')
else:
result_list.append('ERROR')
result_list.append(result)
result_list.append(str(execution_time))
with open('path/to/my/testresults.csv', 'a') as resultFile:
wr = csv.writer(resultFile)
wr.writerow(result_list)