add HelloWorld example

This commit is contained in:
debugtalk
2018-02-09 16:43:50 +08:00
parent 3936b82a52
commit 3e0d117a05
10 changed files with 656 additions and 0 deletions

View File

@@ -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)

View File

@@ -0,0 +1,205 @@
<!DOCTYPE html>
<html>
<head>
<title>Test Result</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2 class="text-capitalize">Test Result</h2>
<p class='attribute'><strong>Start Time: </strong>2018-02-09 16:25:53</p>
<p class='attribute'><strong>Duration: </strong>0.141s</p>
<p class='attribute'><strong>Status: </strong>Pass: 11</p>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-10 col-md-10">
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>smoketest</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr class='success'>
<td class="col-xs-9">get token</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">reset all users</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">make sure user 1000 does not exist</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">create user 1000</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">check if user 1000 exists</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">make sure user 1001 does not exist</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">create user 1001</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">check if user 1001 exists</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">make sure user 1002 does not exist</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">create user 1002</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">check if user 1002 exists</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr>
<td>
Total Test Runned: 11
</td>
<td>
<span>Pass: 11</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('td').on('click', '.btn', function(e){
e.preventDefault();
var $this = $(this);
var $nextRow = $this.closest('tr').next('tr');
$nextRow.slideToggle("fast");
$this.text(function(i, text){
if (text === 'View') {
return 'Hide';
} else {
return 'View';
};
});
});
});
</script>
</body>
</html>

View File

@@ -0,0 +1,166 @@
<!DOCTYPE html>
<html>
<head>
<title>Test Result</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2 class="text-capitalize">Test Result</h2>
<p class='attribute'><strong>Start Time: </strong>2018-02-09 16:38:14</p>
<p class='attribute'><strong>Duration: </strong>0.092s</p>
<p class='attribute'><strong>Status: </strong>Pass: 8</p>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-10 col-md-10">
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>smoketest</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr class='success'>
<td class="col-xs-9">get token</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">reset all users</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">make sure user 1000 does not exist</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">create user 1000</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">check if user 1000 exists</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">make sure user 1001 does not exist</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">create user 1001</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr class='success'>
<td class="col-xs-9">check if user 1001 exists</td>
<td class="col-xs-3">
<span class="label label-success">
Pass
</span>
</td>
</tr>
<tr>
<td>
Total Test Runned: 8
</td>
<td>
<span>Pass: 8</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('td').on('click', '.btn', function(e){
e.preventDefault();
var $this = $(this);
var $nextRow = $this.closest('tr').next('tr');
$nextRow.slideToggle("fast");
$this.text(function(i, text){
if (text === 'View') {
return 'Hide';
} else {
return 'View';
};
});
});
});
</script>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

View File

View File

@@ -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]

View File

@@ -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

View File

@@ -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]

View File

@@ -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)

View File

@@ -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)