运行 python 中的所有测试文件来自使用 "assert" 的一个函数,测试失败时不退出 "for loop"
Running all test files in python from one function using "assert" without exiting "for loop" on test failure
我正在研究从给定目录读取“.test”文件的测试运行器。
“.test”的结构是:
---TEMPLATE---
template content
---CONTEXT---
context to render on template
---RESULT---
expected result of the template.
有'n'个没有。我的测试目录中的测试文件。
我存储测试号。字典中 .test 文件的数量 "tests" 作为其键,.test 文件的名称作为其值。
之后迭代字典 "tests" 并读取 .test 文件的内容并将它们存储在变量中。
---TEMPLATE--- part in "template_string",
---CONTEXT--- part in "context_string", and
---RESULT--- part in "expected_result"
然后使用 jinja2.Environment class 渲染 template_string 和 context_string 并将它们存储在 "result" 变量中。
比较 "result" 与 "expected_result"。
当前测试运行器代码:
class TestTempates(TestCase):
def test_method(self):
tests = { dictionary of .test file }
results = {} #to store status of test case at there index (pass or error).
env = jinja2.Environment()
passed = 0
error = 0
for index, fname in tests.items():
file_path = dirpath + os.sep + fname
with open(file_path) as f:
# logic to read file content
template_string = #content of ---TEMPLATE--- part from file
context_string = #content of ---CONTEXT--- part from file
expected_result = #content of ---RESULT--- part from file
template = env.from_string(template_string)
context = json.loads(context_string)
result = template.render(context)
if result == expected_result:
results[index] = "Pass"
passed += 1
else:
sep = "-----------------------------"
error = "Error: results mismatch:\n%s\n%s\n%s\n%s" % \
(sep, result, sep, expected_result)
results[index] = error
errors += 1
将 "result" 和 "expected_result" 与 "if else" 条件进行比较工作正常。
但是现在我想在任何测试文件 "result" 与 "expected_result" 不匹配时使用 "assert" 或 "assertEquals" 而不退出 "for loop" 直到所有测试文件都不匹配执行。
因此,我可以在 Travis CI 中使用我的测试运行程序,这样当任何测试用例失败时,Travis 构建就会失败。
在当前情况下,Travis CI 构建不会因测试用例失败而失败。
您可以按照下面的代码片段来解决您的问题。
suite = unittest.TestSuite()
def test_main(self):
self.assertEquals(self.result, self.expected_output)
def test_method(self):
"""
"""
# -- code to get tests objects to have all .tests content
for index, fname in tests.items():
# get result and expected_output value
obj = type('Test', (unittest.TestCase,), {'test_main': test_main,
'result':result, 'expected_output':expected_output})
suite.addTest(obj('test_main'))
unittest.TextTestRunner(verbosity=2).run(suite)
我正在研究从给定目录读取“.test”文件的测试运行器。
“.test”的结构是:
---TEMPLATE---
template content
---CONTEXT---
context to render on template
---RESULT---
expected result of the template.
有'n'个没有。我的测试目录中的测试文件。 我存储测试号。字典中 .test 文件的数量 "tests" 作为其键,.test 文件的名称作为其值。
之后迭代字典 "tests" 并读取 .test 文件的内容并将它们存储在变量中。
---TEMPLATE--- part in "template_string",
---CONTEXT--- part in "context_string", and
---RESULT--- part in "expected_result"
然后使用 jinja2.Environment class 渲染 template_string 和 context_string 并将它们存储在 "result" 变量中。
比较 "result" 与 "expected_result"。
当前测试运行器代码:
class TestTempates(TestCase):
def test_method(self):
tests = { dictionary of .test file }
results = {} #to store status of test case at there index (pass or error).
env = jinja2.Environment()
passed = 0
error = 0
for index, fname in tests.items():
file_path = dirpath + os.sep + fname
with open(file_path) as f:
# logic to read file content
template_string = #content of ---TEMPLATE--- part from file
context_string = #content of ---CONTEXT--- part from file
expected_result = #content of ---RESULT--- part from file
template = env.from_string(template_string)
context = json.loads(context_string)
result = template.render(context)
if result == expected_result:
results[index] = "Pass"
passed += 1
else:
sep = "-----------------------------"
error = "Error: results mismatch:\n%s\n%s\n%s\n%s" % \
(sep, result, sep, expected_result)
results[index] = error
errors += 1
将 "result" 和 "expected_result" 与 "if else" 条件进行比较工作正常。 但是现在我想在任何测试文件 "result" 与 "expected_result" 不匹配时使用 "assert" 或 "assertEquals" 而不退出 "for loop" 直到所有测试文件都不匹配执行。 因此,我可以在 Travis CI 中使用我的测试运行程序,这样当任何测试用例失败时,Travis 构建就会失败。
在当前情况下,Travis CI 构建不会因测试用例失败而失败。
您可以按照下面的代码片段来解决您的问题。
suite = unittest.TestSuite()
def test_main(self):
self.assertEquals(self.result, self.expected_output)
def test_method(self):
"""
"""
# -- code to get tests objects to have all .tests content
for index, fname in tests.items():
# get result and expected_output value
obj = type('Test', (unittest.TestCase,), {'test_main': test_main,
'result':result, 'expected_output':expected_output})
suite.addTest(obj('test_main'))
unittest.TextTestRunner(verbosity=2).run(suite)