From 8b0b7e7efd0112835131829c624fb48f2e24cd4a Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 24 Feb 2018 17:38:47 +0800 Subject: [PATCH] add HttpRunner class as interface --- httprunner/__init__.py | 4 +++- httprunner/cli.py | 27 ++------------------------- httprunner/task.py | 30 ++++++++++++++++++++++++++++++ tests/test_runner.py | 23 +++++++++++------------ 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/httprunner/__init__.py b/httprunner/__init__.py index 17962017..bf90de5d 100644 --- a/httprunner/__init__.py +++ b/httprunner/__init__.py @@ -1 +1,3 @@ -__version__ = '0.9.3c' \ No newline at end of file +__version__ = '0.9.4' + +from httprunner.task import HttpRunner diff --git a/httprunner/cli.py b/httprunner/cli.py index b07374c6..2270acf4 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -7,35 +7,12 @@ from collections import OrderedDict from httprunner import __version__ as hrun_version from httprunner import logger -from httprunner.exception import TestcaseNotFound -from httprunner.task import Result, TaskSuite +from httprunner.task import HttpRunner from httprunner.utils import create_scaffold, print_output, string_type from pyunitreport import __version__ as pyu_version from pyunitreport import HTMLTestRunner -def run_suite_path(path, mapping=None, runner=None): - """ run suite with YAML/JSON file path - @params: - - path: testset path - - mapping: passed in variables mapping, it will override variables in config block - - runner: HTMLTestRunner() or TextTestRunner() - """ - try: - mapping = mapping or {} - task_suite = TaskSuite(path, mapping) - except TestcaseNotFound: - sys.exit(1) - - test_runner = runner or unittest.TextTestRunner() - result = test_runner.run(task_suite) - - output = {} - for task in task_suite.tasks: - output.update(task.output) - - return Result(result, output) - def main_hrun(): """ API test: parse command line options and run commands. """ @@ -76,7 +53,7 @@ def main_hrun(): "failfast": args.failfast } test_runner = HTMLTestRunner(**kwargs) - result = run_suite_path(args.testset_paths, {}, test_runner) + result = HttpRunner(args.testset_paths, test_runner).run() print_output(result.output) return 0 if result.success else 1 diff --git a/httprunner/task.py b/httprunner/task.py index b3db2be6..6057d2e8 100644 --- a/httprunner/task.py +++ b/httprunner/task.py @@ -1,3 +1,4 @@ +import sys import unittest from httprunner import exception, logger, runner, testcase, utils @@ -153,6 +154,35 @@ class Result(object): return self.Stat(**stat) +class HttpRunner(object): + + def __init__(self, path, runner=None): + """ initialize HttpRunner with specified testset file path and test runner + @params: + - path: YAML/JSON testset file path + - runner: HTMLTestRunner() or TextTestRunner() + """ + self.path = path + self.runner = runner or unittest.TextTestRunner() + + def run(self, mapping=None): + """ start to run suite + if mapping specified, it will override variables in config block + """ + try: + mapping = mapping or {} + task_suite = TaskSuite(self.path, mapping) + except exception.TestcaseNotFound: + sys.exit(1) + + result = self.runner.run(task_suite) + output = {} + for task in task_suite.tasks: + output.update(task.output) + + return Result(result, output) + + class LocustTask(object): def __init__(self, path, locust_client, mapping=None): diff --git a/tests/test_runner.py b/tests/test_runner.py index 55de6d05..017bbee1 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -1,8 +1,7 @@ import os import time -from httprunner import exception, runner, testcase, utils -from httprunner.cli import run_suite_path +from httprunner import HttpRunner, exception, runner, testcase, utils from tests.base import ApiServerUnittest @@ -78,11 +77,11 @@ class TestRunner(ApiServerUnittest): def test_run_testset_hardcode(self): for testcase_file_path in self.testcase_file_path_list: - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) def test_run_testsets_hardcode(self): - result = run_suite_path(self.testcase_file_path_list) + result = HttpRunner(self.testcase_file_path_list).run() self.assertTrue(result.success) self.assertEqual(result.stat.total, 6) self.assertEqual(result.stat.successes, 6) @@ -90,37 +89,37 @@ class TestRunner(ApiServerUnittest): def test_run_testset_template_variables(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testset_variables.yml') - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) def test_run_testset_template_import_functions(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testset_template_import_functions.yml') - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) def test_run_testsets_template_import_functions(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testset_template_import_functions.yml') - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) def test_run_testsets_template_lambda_functions(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testset_template_lambda_functions.yml') - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) def test_run_testset_layered(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testset_layer.yml') - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) def test_run_testset_output(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testset_layer.yml') - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) self.assertIn("token", result.output) @@ -130,7 +129,7 @@ class TestRunner(ApiServerUnittest): variables_mapping = { "app_version": '2.9.7' } - result = run_suite_path(testcase_file_path, variables_mapping) + result = HttpRunner(testcase_file_path).run(variables_mapping) self.assertTrue(result.success) self.assertIn("token", result.output) @@ -162,7 +161,7 @@ class TestRunner(ApiServerUnittest): def test_run_testset_with_parameters(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_parameters.yml') - result = run_suite_path(testcase_file_path) + result = HttpRunner(testcase_file_path).run() self.assertTrue(result.success) self.assertIn("token", result.output) self.assertEqual(result.stat.total, 6)