Exemplo n.º 1
0
def run_tests():
    result = TestResult()
    print(TermColor.BOLD + 'Running tests ... ' + TermColor.ENDC, end='')
    defaultTestLoader.discover('.').run(result)
    if result.wasSuccessful():
        print(TermColor.OKGREEN + 'Passed' + TermColor.ENDC)
    else:
        print(TermColor.FAIL + 'Failed' + TermColor.ENDC)
    print()

    return result.wasSuccessful()
Exemplo n.º 2
0
def extract_test_result(test_results: unittest.TestResult, **kwargs) -> dict:
    """
    将pyunit中的测试结果进行数据提取和json编码
    :param test_results: 
    :return:
    """

    run_time = kwargs.get('run_time', None)
    pro_id = kwargs.get('pro_id', None)
    pro_version = kwargs.get('pro_version', None)
    tag = kwargs.get('tag', 'default')

    # 主体部分
    res_dict = dict(
        # was_successful=True if test_results.wasSuccessful() else False,
        was_successful=test_results.wasSuccessful(),
        total=test_results.testsRun,
        failures=len(test_results.failures),
        errors=len(test_results.errors),
        skipped=len(test_results.skipped),
        run_time=run_time,
        pro_id=pro_id,
        pro_version=pro_version,
        tag=tag)

    # 详细信息部分
    failure_list = []  # 失败的内容
    for x in test_results.failures:
        test_case = x[0]._testMethodName
        method_doc = x[0]._testMethodDoc  # 给测试脚本写的文档
        assert method_doc is not None, ('请给测试用例%s函数写上文档注释' % test_case)
        explain = method_doc.rstrip('\n        :return:')

        note_data = {
            'test_case': test_case,
            'explain': explain,
            'status': 'failures',
            'note': x[1]
        }

        failure_list.append(note_data)

    for i in test_results.errors:
        test_case = i[0]._testMethodName
        method_doc = i[0]._testMethodDoc  # 给测试脚本写的文档
        assert method_doc is not None, ('请给测试用例%s函数写上文档注释' % test_case)
        explain = method_doc.rstrip('\n        :return:')

        note_data = {
            'test_case': test_case,
            'explain': explain,
            'status': 'errors',
            'note': i[1]
        }
        failure_list.append(note_data)

    res_dict['details'] = failure_list

    return res_dict
Exemplo n.º 3
0
def set_result(test_results: unittest.TestResult, **kwargs):
    """
    提取unittest中的测试结果
    :param test_results: unittest.TestResult
    :return:
    """
    total = test_results.testsRun
    failures = len(test_results.failures)
    errors = len(test_results.errors)
    skipped = len(test_results.skipped)
    success = total - failures - errors - skipped

    # overview
    res_dict = dict(was_successful=test_results.wasSuccessful(),
                    total=total,
                    failures=failures,
                    errors=errors,
                    success=success,
                    skipped=skipped)
    res_dict.update(kwargs)

    # traceback
    failure_list = []  # 失败的内容
    for x in test_results.failures:
        case_name = x[0]._testMethodName
        method_doc = x[0]._testMethodDoc  # 给测试脚本写的文档
        assert method_doc is not None, ('请给测试用例%s函数写上文档注释' % case_name)
        note_data = {
            'case_name':
            case_name,
            'explain':
            method_doc.replace('\n', '').strip().replace('        ', ', '),
            'status':
            'failures',
            'note':
            x[1]
        }
        failure_list.append(note_data)

    for x in test_results.errors:
        case_name = x[0]._testMethodName
        method_doc = x[0]._testMethodDoc  # 给测试脚本写的文档
        assert method_doc is not None, ('请给测试用例%s函数写上文档注释' % case_name)
        note_data = {
            'case_name':
            case_name,
            'explain':
            method_doc.replace('\n', '').strip().replace('        ', ', '),
            'status':
            'errors',
            'note':
            x[1]
        }
        failure_list.append(note_data)

    res_dict['details'] = failure_list

    return res_dict
Exemplo n.º 4
0
    def test_assert_the_real_render_template_is_restored(self):
        test = TestNotRenderTemplates('test_assert_not_process_the_template')
        test_result = TestResult()
        test(test_result)

        assert test_result.wasSuccessful()

        response = self.client.get("/template/")

        assert len(response.data) > 0
Exemplo n.º 5
0
    def test_assert_the_real_render_template_is_restored(self):
        test = TestNotRenderTemplates('test_assert_not_process_the_template')
        test_result = TestResult()
        test(test_result)

        assert test_result.wasSuccessful()

        response = self.client.get("/template/")

        assert "" != response.data
Exemplo n.º 6
0
 def test_data_cleaned_up(self):
     user_collection.remove()
     company_collection.remove()
     loader = TestLoader()
     suite = loader.loadTestsFromTestCase(TestUnittestSupport.TestTestCase)
     result = TestResult()
     suite.run(result)
     self.assertTrue(result.wasSuccessful(), result)
     self.assertEquals(user_collection.count(), 0)
     self.assertEquals(company_collection.count(), 0)
Exemplo n.º 7
0
 def test_data_cleaned_up(self):
     user_collection.remove()
     company_collection.remove()
     loader = TestLoader()
     suite = loader.loadTestsFromTestCase(TestUnittestSupport.TestTestCase)
     result = TestResult()
     suite.run(result)
     self.assertTrue(result.wasSuccessful(), result)
     self.assertEquals(user_collection.count(), 0)
     self.assertEquals(company_collection.count(), 0)
Exemplo n.º 8
0
    def testFilterSomeMatches(self):
        """
        Filtering a test suite on a name that matches just one of two tests
        should give a result that contains just the matching test.
        """
        class Dummy1(TestCase):
            def runTest(self):
                pass

        class Dummy2(TestCase):
            def runTest(self):
                self.fail('Oops')

        orig = TestSuite()
        orig.addTests([Dummy1(), Dummy2()])
        new = filterTestSuite('Dummy1', orig)
        self.assertEqual(1, new.countTestCases())
        # Run the test suite to check that the Dummy2 test was not run.
        result = TestResult()
        new.run(result)
        self.assertTrue(result.wasSuccessful())