mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-19 15:19:32 +08:00
rename test folder to tests
This commit is contained in:
0
tests/data/__init__.py
Normal file
0
tests/data/__init__.py
Normal file
72
tests/data/custom_functions.py
Normal file
72
tests/data/custom_functions.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import hashlib
|
||||
import json
|
||||
import random
|
||||
import string
|
||||
import time
|
||||
|
||||
try:
|
||||
string_type = basestring
|
||||
PYTHON_VERSION = 2
|
||||
import urllib
|
||||
except NameError:
|
||||
string_type = str
|
||||
PYTHON_VERSION = 3
|
||||
import urllib.parse as urllib
|
||||
|
||||
|
||||
def gen_random_string(str_len):
|
||||
return ''.join(
|
||||
random.choice(string.ascii_letters + string.digits) for _ in range(str_len))
|
||||
|
||||
def gen_md5(*args):
|
||||
args = [handle_req_data(item) for item in args]
|
||||
return hashlib.md5("".join(args).encode('utf-8')).hexdigest()
|
||||
|
||||
def handle_req_data(data):
|
||||
|
||||
if PYTHON_VERSION == 3 and isinstance(data, bytes):
|
||||
# In Python3, convert bytes to str
|
||||
data = data.decode('utf-8')
|
||||
|
||||
if not data:
|
||||
return data
|
||||
|
||||
if isinstance(data, str):
|
||||
# check if data in str can be converted to dict
|
||||
try:
|
||||
data = json.loads(data)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if isinstance(data, dict):
|
||||
# sort data in dict with keys, then convert to str
|
||||
data = json.dumps(data, sort_keys=True)
|
||||
|
||||
return data
|
||||
|
||||
def gen_urlencode_str(**kargs):
|
||||
urlencoded_str = ""
|
||||
quote_times = int(kargs.pop("quote_times", 1))
|
||||
|
||||
for key, value in kargs.items():
|
||||
urlencoded_str += key
|
||||
urlencoded_str += "="
|
||||
if value == "undefined":
|
||||
urlencoded_str += "undefined"
|
||||
else:
|
||||
if isinstance(value, (dict, list)):
|
||||
value = json.dumps(value)
|
||||
elif isinstance(value, (int, float)):
|
||||
value = str(value)
|
||||
|
||||
value_str = value.encode('utf-8')
|
||||
for _ in range(quote_times):
|
||||
value_str = urllib.quote_plus(value_str)
|
||||
urlencoded_str += value_str
|
||||
|
||||
urlencoded_str += "&"
|
||||
|
||||
return urlencoded_str.strip("&")
|
||||
|
||||
def get_timestamp():
|
||||
return int(time.time() * 1000)
|
||||
42
tests/data/demo_binds.yml
Normal file
42
tests/data/demo_binds.yml
Normal file
@@ -0,0 +1,42 @@
|
||||
register_variables:
|
||||
variable_binds:
|
||||
- TOKEN: "debugtalk"
|
||||
- var: [1, 2, 3]
|
||||
- data: {'name': 'user', 'password': '123456'}
|
||||
|
||||
register_template_variables:
|
||||
variable_binds:
|
||||
- TOKEN: "debugtalk"
|
||||
- token: $TOKEN
|
||||
|
||||
bind_lambda_functions:
|
||||
function_binds:
|
||||
add_one: "lambda x: x + 1"
|
||||
add_two_nums: "lambda x, y: x + y"
|
||||
variable_binds:
|
||||
- add1: ${add_one(2)}
|
||||
- sum2nums: ${add_two_nums(2, 3)}
|
||||
|
||||
bind_lambda_functions_with_import:
|
||||
requires:
|
||||
- random
|
||||
- string
|
||||
- hashlib
|
||||
function_binds:
|
||||
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
|
||||
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- random: ${gen_random_string(5)}
|
||||
- data: "{'name': 'user', 'password': '123456'}"
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
|
||||
bind_module_functions:
|
||||
function_binds:
|
||||
import_module_functions:
|
||||
- tests.data.custom_functions
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- random: ${gen_random_string(5)}
|
||||
- data: "{'name': 'user', 'password': '123456'}"
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
41
tests/data/demo_import_functions.yml
Normal file
41
tests/data/demo_import_functions.yml
Normal file
@@ -0,0 +1,41 @@
|
||||
- config:
|
||||
name: "create user testsets."
|
||||
import_module_functions:
|
||||
- tests.data.custom_functions
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- json: {}
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $json, $random)}
|
||||
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- json: {"name": "user", "password": "123456"}
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: $authorization
|
||||
random: $random
|
||||
json: $json
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 201}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": true}
|
||||
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- json: {"name": "user", "password": "123456"}
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: $authorization
|
||||
random: $random
|
||||
json: $json
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 500}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": false}
|
||||
52
tests/data/demo_template_separate.yml
Normal file
52
tests/data/demo_template_separate.yml
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
requires:
|
||||
- random
|
||||
- string
|
||||
- hashlib
|
||||
function_binds:
|
||||
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
|
||||
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- random: ${gen_random_string(5)}
|
||||
- data: '{"name": "user", "password": "123456"}'
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: $authorization
|
||||
random: $random
|
||||
data: $data
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 201}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": true}
|
||||
|
||||
- test:
|
||||
name: create user which does exist
|
||||
requires:
|
||||
- random
|
||||
- string
|
||||
- hashlib
|
||||
function_binds:
|
||||
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
|
||||
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- random: ${gen_random_string(5)}
|
||||
- data: '{"name": "user", "password": "123456"}'
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: $authorization
|
||||
random: $random
|
||||
data: $data
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 500}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": false}
|
||||
49
tests/data/demo_template_sets.yml
Normal file
49
tests/data/demo_template_sets.yml
Normal file
@@ -0,0 +1,49 @@
|
||||
- config:
|
||||
name: "create user testsets."
|
||||
requires:
|
||||
- random
|
||||
- string
|
||||
- hashlib
|
||||
function_binds:
|
||||
gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))"
|
||||
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- data: ""
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
request:
|
||||
base_url: http://127.0.0.1:5000
|
||||
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- data: '{"name": "user", "password": "123456"}'
|
||||
request:
|
||||
url: /api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: $authorization
|
||||
random: $random
|
||||
data: $data
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 201}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": true}
|
||||
|
||||
- test:
|
||||
name: create user which does exist
|
||||
variable_binds:
|
||||
- data: '{"name": "user", "password": "123456"}'
|
||||
- expected_status_code: 500
|
||||
request:
|
||||
url: /api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: $authorization
|
||||
random: $random
|
||||
data: $data
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 500}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": false}
|
||||
46
tests/data/simple_demo_auth_hardcode.json
Normal file
46
tests/data/simple_demo_auth_hardcode.json
Normal file
@@ -0,0 +1,46 @@
|
||||
[
|
||||
{
|
||||
"test": {
|
||||
"name": "create user which does not exist",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/users/1000",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
|
||||
"random": "A2dEx"
|
||||
},
|
||||
"json": {
|
||||
"name": "user1",
|
||||
"password": "123456"
|
||||
}
|
||||
},
|
||||
"validators": [
|
||||
{"check": "status_code", "comparator": "eq", "expected": 201},
|
||||
{"check": "content.success", "comparator": "eq", "expected": true}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": {
|
||||
"name": "create user which existed",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/users/1000",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
|
||||
"random": "A2dEx"
|
||||
},
|
||||
"json": {
|
||||
"name": "user1",
|
||||
"password": "123456"
|
||||
}
|
||||
},
|
||||
"validators": [
|
||||
{"check": "status_code", "comparator": "eq", "expected": 500},
|
||||
{"check": "content.success", "comparator": "eq", "expected": false}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
31
tests/data/simple_demo_auth_hardcode.yml
Normal file
31
tests/data/simple_demo_auth_hardcode.yml
Normal file
@@ -0,0 +1,31 @@
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: a83de0ff8d2e896dbd8efb81ba14e17d
|
||||
random: A2dEx
|
||||
json:
|
||||
name: "user1"
|
||||
password: "123456"
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 201}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": true}
|
||||
|
||||
- test:
|
||||
name: create user which existed
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
authorization: a83de0ff8d2e896dbd8efb81ba14e17d
|
||||
random: A2dEx
|
||||
json:
|
||||
name: "user1"
|
||||
password: "123456"
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 500}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": false}
|
||||
43
tests/data/simple_demo_no_auth.json
Normal file
43
tests/data/simple_demo_no_auth.json
Normal file
@@ -0,0 +1,43 @@
|
||||
[
|
||||
{
|
||||
"test": {
|
||||
"name": "create user which does not exist",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/users/1000",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"cookies": {},
|
||||
"json": {
|
||||
"name": "user1",
|
||||
"password": "123456"
|
||||
}
|
||||
},
|
||||
"validators": [
|
||||
{"check": "status_code", "comparator": "eq", "expected": 201},
|
||||
{"check": "content.success", "comparator": "eq", "expected": true}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"test": {
|
||||
"name": "create user which existed",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/users/1000",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"json": {
|
||||
"name": "user1",
|
||||
"password": "123456"
|
||||
}
|
||||
},
|
||||
"validators": [
|
||||
{"check": "status_code", "comparator": "eq", "expected": 500},
|
||||
{"check": "content.success", "comparator": "eq", "expected": false}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
29
tests/data/simple_demo_no_auth.yml
Normal file
29
tests/data/simple_demo_no_auth.yml
Normal file
@@ -0,0 +1,29 @@
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
json:
|
||||
name: user1
|
||||
password: 123456
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 201}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": true}
|
||||
- {"check": "headers.content-type", "comparator": "eq", "expected": "application/json"}
|
||||
|
||||
- test:
|
||||
name: create user which existed
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
json:
|
||||
name: user1
|
||||
password: 123456
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 500}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": false}
|
||||
- {"check": "headers.content-type", "comparator": "eq", "expected": "application/json"}
|
||||
Reference in New Issue
Block a user