diff --git a/examples/HelloWorld/README.md b/examples/HelloWorld/README.md new file mode 100644 index 00000000..f7efe8a2 --- /dev/null +++ b/examples/HelloWorld/README.md @@ -0,0 +1,98 @@ +# Hello World + +This example shows you how to organize and run testcases in layer. + +## file structure + +According to rules, all testcase definition files should be placed in `tests` folder, and testing reports will be generated in `reports` folder. + +```text +$ cd httprunner/examples/HelloWorld +$ tree . +. +├── README.md +├── reports +│ └── smoketest +│ └── 2018-02-09-16-25-54.html +└── tests + ├── __init__.py + ├── api + │ └── basic.yml + ├── debugtalk.py + ├── suite + │ ├── create_and_check.yml + │ └── setup.yml + └── testcases + └── smoketest.yml +``` + +## Start server + +In order to run test, we need a backend service, and here we will use `api_server` located in our unittests. + +```bash +$ cd httprunner +$ export FLASK_APP=tests/api_server.py +$ flask run + * Serving Flask app "tests.api_server" + * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) +``` + +## run testcases + +When you want to run testcases, you should make sure you are in the root directory of your project. In this example, that is the HelloWorld folder path. + +```bash +$ cd httprunner/examples/HelloWorld +``` + +Then, run the testcase with `hrun` command. + +``` +$ hrun tests/testcases/smoketest.yml +Running tests... +---------------------------------------------------------------------- + get token ... INFO:root: Start to POST http://127.0.0.1:5000/api/get-token +INFO:root: status_code: 200, response_time: 12 ms, response_length: 46 bytes +OK (0.018492)s + reset all users ... INFO:root: Start to GET http://127.0.0.1:5000/api/reset-all +INFO:root: status_code: 200, response_time: 5 ms, response_length: 17 bytes +OK (0.006153)s + make sure user 1000 does not exist ... INFO:root: Start to GET http://127.0.0.1:5000/api/users/1000 +ERROR:root: Failed to GET http://127.0.0.1:5000/api/users/1000! exception msg: 404 Client Error: NOT FOUND for url: http://127.0.0.1:5000/api/users/1000 +OK (0.010638)s + create user 1000 ... INFO:root: Start to POST http://127.0.0.1:5000/api/users/1000 +INFO:root: status_code: 201, response_time: 9 ms, response_length: 54 bytes +OK (0.010303)s + check if user 1000 exists ... INFO:root: Start to GET http://127.0.0.1:5000/api/users/1000 +INFO:root: status_code: 200, response_time: 11 ms, response_length: 66 bytes +OK (0.013168)s + make sure user 1001 does not exist ... INFO:root: Start to GET http://127.0.0.1:5000/api/users/1001 +ERROR:root: Failed to GET http://127.0.0.1:5000/api/users/1001! exception msg: 404 Client Error: NOT FOUND for url: http://127.0.0.1:5000/api/users/1001 +OK (0.013631)s + create user 1001 ... INFO:root: Start to POST http://127.0.0.1:5000/api/users/1001 +INFO:root: status_code: 201, response_time: 6 ms, response_length: 54 bytes +OK (0.007490)s + check if user 1001 exists ... INFO:root: Start to GET http://127.0.0.1:5000/api/users/1001 +INFO:root: status_code: 200, response_time: 9 ms, response_length: 66 bytes +OK (0.011435)s + +---------------------------------------------------------------------- +Ran 8 tests in 0.092s + +OK + + + +Generating HTML reports... +Template is not specified, load default template instead. +Reports generated: /Users/debugtalk/MyProjects/HttpRunner-dev/HttpRunner/examples/HelloWorld/reports/smoketest/2018-02-09-16-38-14.html +``` + +After the running is over, you will get a testing report, which is in HTML format. + +```bash +$ open reports/smoketest/2018-02-09-16-38-14.html +``` + +![](test-report.jpg) diff --git a/examples/HelloWorld/reports/smoketest/2018-02-09-16-25-54.html b/examples/HelloWorld/reports/smoketest/2018-02-09-16-25-54.html new file mode 100644 index 00000000..86ad6698 --- /dev/null +++ b/examples/HelloWorld/reports/smoketest/2018-02-09-16-25-54.html @@ -0,0 +1,205 @@ + + + + Test Result + + + + + +
+
+
+

Test Result

+

Start Time: 2018-02-09 16:25:53

+

Duration: 0.141s

+

Status: Pass: 11

+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
smoketestStatus
get token + + + Pass + + + +
reset all users + + + Pass + + + +
make sure user 1000 does not exist + + + Pass + + + +
create user 1000 + + + Pass + + + +
check if user 1000 exists + + + Pass + + + +
make sure user 1001 does not exist + + + Pass + + + +
create user 1001 + + + Pass + + + +
check if user 1001 exists + + + Pass + + + +
make sure user 1002 does not exist + + + Pass + + + +
create user 1002 + + + Pass + + + +
check if user 1002 exists + + + Pass + + + +
+ Total Test Runned: 11 + + Pass: 11 +
+
+
+
+ + + + \ No newline at end of file diff --git a/examples/HelloWorld/reports/smoketest/2018-02-09-16-38-14.html b/examples/HelloWorld/reports/smoketest/2018-02-09-16-38-14.html new file mode 100644 index 00000000..8df2409b --- /dev/null +++ b/examples/HelloWorld/reports/smoketest/2018-02-09-16-38-14.html @@ -0,0 +1,166 @@ + + + + Test Result + + + + + +
+
+
+

Test Result

+

Start Time: 2018-02-09 16:38:14

+

Duration: 0.092s

+

Status: Pass: 8

+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
smoketestStatus
get token + + + Pass + + + +
reset all users + + + Pass + + + +
make sure user 1000 does not exist + + + Pass + + + +
create user 1000 + + + Pass + + + +
check if user 1000 exists + + + Pass + + + +
make sure user 1001 does not exist + + + Pass + + + +
create user 1001 + + + Pass + + + +
check if user 1001 exists + + + Pass + + + +
+ Total Test Runned: 8 + + Pass: 8 +
+
+
+
+ + + + \ No newline at end of file diff --git a/examples/HelloWorld/test-report.jpg b/examples/HelloWorld/test-report.jpg new file mode 100644 index 00000000..245a4db6 Binary files /dev/null and b/examples/HelloWorld/test-report.jpg differ diff --git a/examples/HelloWorld/tests/__init__.py b/examples/HelloWorld/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/HelloWorld/tests/api/basic.yml b/examples/HelloWorld/tests/api/basic.yml new file mode 100644 index 00000000..b56925f6 --- /dev/null +++ b/examples/HelloWorld/tests/api/basic.yml @@ -0,0 +1,82 @@ +- api: + def: get_token($user_agent, $device_sn, $os_platform, $app_version) + request: + url: /api/get-token + method: POST + headers: + user_agent: $user_agent + device_sn: $device_sn + os_platform: $os_platform + app_version: $app_version + json: + sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)} + validate: + - eq: ["status_code", 200] + - len_eq: ["content.token", 16] + +- api: + def: create_user($uid, $user_name, $user_password, $token) + request: + url: /api/users/$uid + method: POST + headers: + token: $token + json: + name: $user_name + password: $user_password + validate: + - eq: ["status_code", 201] + +- api: + def: get_user($uid, $token) + request: + url: /api/users/$uid + method: GET + headers: + token: $token + validate: + - eq: ["status_code", 200] + +- api: + def: update_user($uid, $user_name, $user_password, $token) + request: + url: /api/users/$uid + method: PUT + headers: + token: $token + json: + name: $user_name + password: $user_password + validate: + - eq: ["status_code", 200] + +- api: + def: delete_user($uid, $token) + request: + url: /api/users/$uid + method: DELETE + headers: + token: $token + validate: + - eq: ["status_code", 200] + +- api: + def: get_users($token) + request: + url: /api/users + method: GET + headers: + token: $token + validate: + - eq: ["status_code", 200] + +- api: + def: reset_all($token) + request: + url: /api/reset-all + method: GET + headers: + token: $token + validate: + - eq: ["status_code", 200] + - eq: ["content.success", true] diff --git a/examples/HelloWorld/tests/debugtalk.py b/examples/HelloWorld/tests/debugtalk.py new file mode 100644 index 00000000..c215984d --- /dev/null +++ b/examples/HelloWorld/tests/debugtalk.py @@ -0,0 +1,28 @@ +import hashlib +import hmac +import random +import string + +SECRET_KEY = "DebugTalk" +default_request = { + "base_url": "http://127.0.0.1:5000", + "headers": { + "Content-Type": "application/json", + "device_sn": "$device_sn" + } +} + +def gen_random_string(str_len): + random_char_list = [] + for _ in range(str_len): + random_char = random.choice(string.ascii_letters + string.digits) + random_char_list.append(random_char) + + random_string = ''.join(random_char_list) + return random_string + +def get_sign(*args): + content = ''.join(args).encode('ascii') + sign_key = SECRET_KEY.encode('ascii') + sign = hmac.new(sign_key, content, hashlib.sha1).hexdigest() + return sign diff --git a/examples/HelloWorld/tests/suite/create_and_check.yml b/examples/HelloWorld/tests/suite/create_and_check.yml new file mode 100644 index 00000000..e1d3e7e8 --- /dev/null +++ b/examples/HelloWorld/tests/suite/create_and_check.yml @@ -0,0 +1,31 @@ + +- config: + name: "create user and check result." + def: create_and_check($uid, $token) + request: $default_request + output: + - token + +- test: + name: make sure user $uid does not exist + api: get_user($uid, $token) + validate: + - eq: ["status_code", 404] + - eq: ["content.success", false] + +- test: + name: create user $uid + variables: + - user_name: "user1" + - user_password: "123456" + api: create_user($uid, $user_name, $user_password, $token) + validate: + - eq: ["status_code", 201] + - eq: ["content.success", true] + +- test: + name: check if user $uid exists + api: get_user($uid, $token) + validate: + - eq: ["status_code", 200] + - eq: ["content.success", true] diff --git a/examples/HelloWorld/tests/suite/setup.yml b/examples/HelloWorld/tests/suite/setup.yml new file mode 100644 index 00000000..beb89182 --- /dev/null +++ b/examples/HelloWorld/tests/suite/setup.yml @@ -0,0 +1,26 @@ +- config: + name: "setup and reset all." + def: setup_and_reset($device_sn) + variables: + - user_agent: 'iOS/10.3' + - device_sn: ${gen_random_string(15)} + - os_platform: 'ios' + - app_version: '2.8.6' + request: $default_request + output: + - token + +- test: + name: get token + api: get_token($user_agent, $device_sn, $os_platform, $app_version) + variables: + - user_agent: 'iOS/10.3' + - device_sn: $device_sn + - os_platform: 'ios' + - app_version: '2.8.6' + extract: + - token: content.token + +- test: + name: reset all users + api: reset_all($token) diff --git a/examples/HelloWorld/tests/testcases/smoketest.yml b/examples/HelloWorld/tests/testcases/smoketest.yml new file mode 100644 index 00000000..e2a22d5b --- /dev/null +++ b/examples/HelloWorld/tests/testcases/smoketest.yml @@ -0,0 +1,20 @@ +- config: + name: smoketest + variables: + - device_sn: ${gen_random_string(15)} + request: $default_request + +- test: + name: setup and reset all. + suite: setup_and_reset($device_sn) + output: + - token + - device_sn + +- test: + name: create user 1000 and check result. + suite: create_and_check(1000, $token) + +- test: + name: create user 1001 and check result. + suite: create_and_check(1001, $token)