Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从一个函数运行python中的所有测试文件,使用;断言;不退出“;for loop";关于测试失败_Python_Unit Testing_Travis Ci_Testcase - Fatal编程技术网

从一个函数运行python中的所有测试文件,使用;断言;不退出“;for loop";关于测试失败

从一个函数运行python中的所有测试文件,使用;断言;不退出“;for loop";关于测试失败,python,unit-testing,travis-ci,testcase,Python,Unit Testing,Travis Ci,Testcase,我正在开发TestRunner,它从给定目录读取“.test”文件 “.test”的结构是: 我的测试目录中有'n'个测试文件。 我将.test文件的test编号存储在字典“tests”中作为它的键,.test文件的名称作为它的值 然后在字典“tests”上迭代,读取.test文件的内容并将其存储在变量中 ---TEMPLATE--- part in "template_string", ---CONTEXT--- part in "context_string", and ---RESULT-

我正在开发TestRunner,它从给定目录读取“.test”文件

“.test”的结构是:

我的测试目录中有'n'个测试文件。 我将.test文件的test编号存储在字典“tests”中作为它的键,.test文件的名称作为它的值

然后在字典“tests”上迭代,读取.test文件的内容并将其存储在变量中

---TEMPLATE--- part in "template_string",
---CONTEXT--- part in "context_string", and
---RESULT--- part in "expected_result"
然后使用jinja2.Environment类呈现带有上下文字符串的模板字符串,并将它们存储在“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
将“结果”和“预期结果”与“如果其他”条件进行比较,效果良好。 但现在我想使用“assert”或“assertEquals”,当任何测试文件“result”与“expected_result”不匹配时,不退出“for循环”,直到所有测试文件都没有执行为止。 所以,我可以在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)
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)