• ok 仔细研究一下

  • 你的分析好像漏了最重要的一步,怎么把对应的 yaml 映射成一个 test 方法,并且添加进 testsuit 中
    在./httprunner/api.py 文件中的这个方法应该才是给对应的配置文件绑定一个测试方法的核心吧。我是没有看懂这个方法的运行原理。。。

    def _add_tests(self, testcases):
            """ initialize testcase with Runner() and add to test suite.
    
            Args:
                testcases (list): testcases list.
    
            Returns:
                unittest.TestSuite()
    
            """
            def _add_test(test_runner, test_dict):
                """ add test to testcase.
                """
                def test(self):
                    try:
                        test_runner.run_test(test_dict)
                    except exceptions.MyBaseFailure as ex:
                        self.fail(str(ex))
                    finally:
                        self.meta_datas = test_runner.meta_datas
    
                if "config" in test_dict:
                    # run nested testcase
                    test.__doc__ = test_dict["config"].get("name")
                    variables = test_dict["config"].get("variables", {})
                else:
                    # run api test
                    test.__doc__ = test_dict.get("name")
                    variables = test_dict.get("variables", {})
    
                if isinstance(test.__doc__, parser.LazyString):
                    try:
                        parsed_variables = parser.parse_variables_mapping(variables)
                        test.__doc__ = parser.parse_lazy_data(
                            test.__doc__, parsed_variables
                        )
                    except exceptions.VariableNotFound:
                        test.__doc__ = str(test.__doc__)
    
                return test
    
            test_suite = unittest.TestSuite()
            for testcase in testcases:
                config = testcase.get("config", {})
                test_runner = runner.Runner(config)
                TestSequense = type('TestSequense', (unittest.TestCase,), {})
    
                tests = testcase.get("teststeps", [])
                for index, test_dict in enumerate(tests):
                    times = test_dict.get("times", 1)
                    try:
                        times = int(times)
                    except ValueError:
                        raise exceptions.ParamsError(
                            "times should be digit, given: {}".format(times))
    
                    for times_index in range(times):
                        # suppose one testcase should not have more than 9999 steps,
                        # and one step should not run more than 999 times.
                        test_method_name = 'test_{:04}_{:03}'.format(index, times_index)
                        test_method = _add_test(test_runner, test_dict)
                        setattr(TestSequense, test_method_name, test_method)
    
                loaded_testcase = self.test_loader.loadTestsFromTestCase(TestSequense)
                setattr(loaded_testcase, "config", config)
                setattr(loaded_testcase, "teststeps", tests)
                setattr(loaded_testcase, "runner", test_runner)
                test_suite.addTest(loaded_testcase)
    
            return test_suite
    
  • --timeout-sec 超时时间控制不了执行时间设置--timeout-sec 1000 以后还是执行 60s