def run_ta_tests(self, ta_path, prod_path, assignment):
        sys.path.insert(0, prod_path)

        nbr_tests_per_class = {}
        test_suite = unittest.TestLoader().discover(ta_path, '*.py')

        for test in test_suite:                                     # Counts number of tests in acceptance test files
            if unittest.suite._isnotsuite(test):
              nbr_tests_per_class[test._test.shortDescription()] = 1
            else:
                for t in test:
                    str_of_testsuite = str(t)
                    testsuite_parts = str_of_testsuite.split("<")
                    if len(testsuite_parts) > 2:
                        class_names = testsuite_parts[2].split(".")
                        class_name = class_names[0].lower()
                        nbr_tests_per_class[class_name] = len(testsuite_parts) - 2
                    else:
                        class_names = testsuite_parts[0].split("(")
                        class_name = class_names[0].lower()
                        nbr_tests_per_class[class_name] = "ModuleImportFailure"

        with open(os.path.join(ta_path + assignment + ".TAreport"), "a+") as ta_reportout_file:
            try:
                # os.system("python " + prod_path + os.sep +"microservice.py")
                ta_reportout_file.write("\n\rStudent submission path:  " + prod_path + "\n\r")
                try:
                    with open(os.path.join(ta_path + assignment + ".stream"), "a+") as ta_stream_file:
                        ta_stream_file.write("/n******************************************************************************************\n")
                        ta_stream_file.write("***  Student submission path:  " + prod_path + "\n")
                        ta_stream_file.write("******************************************************************************************\n")
                        ta_report = TextTestRunner(stream=ta_stream_file, verbosity=2).run(test_suite)
                except Exception as e:
                    ta_reportout_file.write("Exception thrown:  " + str(e))
                ta_reportout_file.write("Number of tests run:  " + str(ta_report.testsRun) + "\n\r")
                if ta_report.wasSuccessful():
                    ta_reportout_file.write("All tests completed successfully!  Success rate 100%\n\r")
                else:
                    ta_stats = self.collect_report_stats(ta_report)

                    pass_ratio = ta_stats.passing_test_ratio() * 100
                    ta_reportout_file.write("See log for Content of TestRunner failures.  "
                                            "\nTotal Failures:  " + str(ta_stats.total_tests_failed) +
                                            "\tErrors:  " + str(ta_stats.total_tests_with_error) +
                                            "\tSuccess rate " + format(pass_ratio, ".2f") + "%\r\n")
                    for err_class, count in ta_stats.total_fails_by_testclass.items():
                        if err_class.lower() in nbr_tests_per_class:
                            total_tests_for_class = nbr_tests_per_class[err_class.lower()]
                            pass_ratio = ((total_tests_for_class - count) / float(total_tests_for_class)) * 100
                            ta_reportout_file.write("\nAll Test Failures for " + err_class +
                                                "  :  " + str(count) +
                                                "\tSuccess rate " + format(pass_ratio, ".2f") + "%\r")

            except Exception as e:
                ta_reportout_file.write("Import Error:  " + prod_path + "\n\r" +
                                        "Exception Message:  " + str(e))

            ta_reportout_file.write("\n********************************************************************************\n\r")
Example #2
0
def test_package(*package_names):
    tests = []
    test_loader = TestLoader()
    for module_name, module in sys.modules.items():
        for package_name in package_names:
            if module_name.startswith('{}.'.format(package_name)):
                module_tests = test_loader.loadTestsFromModule(module)
                module_tests = [t for t in module_tests if is_test_suite_loaded(t)]
                tests.extend(module_tests)
    test_result = TextTestRunner(failfast=True, resultclass=TimedTextTestResult).run(TestSuite(tests))
    if not test_result.wasSuccessful():
        raise Exception('test failed')
Example #3
0
def test_package(*package_names):
    tests = []
    test_loader = TestLoader()
    for module_name, module in sys.modules.items():
        for package_name in package_names:
            if module_name.startswith('{}.'.format(package_name)):
                module_tests = test_loader.loadTestsFromModule(module)
                module_tests = [
                    t for t in module_tests if is_test_suite_loaded(t)
                ]
                tests.extend(module_tests)
                break
    test_result = TextTestRunner(failfast=True,
                                 resultclass=TimedTextTestResult).run(
                                     TestSuite(tests))
    if not test_result.wasSuccessful():
        raise Exception('test failed')