From 722555bafb5719b5b207c407f0f14cb758a79bda Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 14 Jul 2019 23:22:17 +0800 Subject: [PATCH] fix #551: raise if times is not digit --- CHANGELOG.md | 1 + httprunner/api.py | 9 ++++++++- tests/test_api.py | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a99c16a..c9b6c6e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ **Fixed** - fix #574: delete unnecessary code +- fix #551: raise if times is not digit ## 2.2.3 (2019-06-30) diff --git a/httprunner/api.py b/httprunner/api.py index a2eca860..0a70277e 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -85,7 +85,14 @@ class HttpRunner(object): tests = testcase.get("teststeps", []) for index, test_dict in enumerate(tests): - for times_index in range(int(test_dict.get("times", 1))): + 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) diff --git a/tests/test_api.py b/tests/test_api.py index c55feb5c..2f0fa5ce 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -4,7 +4,7 @@ import shutil import time import unittest -from httprunner import loader, parser +from httprunner import exceptions, loader, parser from httprunner.api import HttpRunner, prepare_locust_tests from tests.api_server import HTTPBIN_SERVER from tests.base import ApiServerUnittest @@ -80,6 +80,39 @@ class TestHttpRunner(ApiServerUnittest): self.assertEqual(self.runner.summary["stat"]["testcases"]["total"], 1) self.assertEqual(self.runner.summary["stat"]["teststeps"]["total"], 10) + def test_text_run_times_invalid(self): + testcases = [ + { + "config": { + 'name': "post data", + 'variables': [] + }, + "teststeps": [ + { + "name": "post data", + "times": "1.5", + "request": { + "url": "{}/post".format(HTTPBIN_SERVER), + "method": "POST", + "headers": { + "User-Agent": "python-requests/2.18.4", + "Content-Type": "application/json" + }, + "data": "abc" + }, + "validate": [ + {"eq": ["status_code", 200]} + ] + } + ] + } + ] + tests_mapping = { + "testcases": testcases + } + with self.assertRaises(exceptions.ParamsError): + self.runner.run_tests(tests_mapping) + def test_text_skip(self): self.runner.run(self.testcase_cli_path) self.assertEqual(self.runner.summary["stat"]["teststeps"]["skipped"], 4)