feat: support api layer and global headers for testcase

This commit is contained in:
xucong053
2022-03-15 12:39:41 +08:00
parent 3b9e6cd736
commit af6c433069
37 changed files with 907 additions and 135 deletions

34
examples/api/get.json Normal file
View File

@@ -0,0 +1,34 @@
{
"name": "",
"request": {
"method": "GET",
"url": "/get",
"params": {
"foo1": "bar1",
"foo2": "bar2"
},
"headers": {
"Postman-Token": "ea19464c-ddd4-4724-abe9-5e2b254c2723"
}
},
"validate": [
{
"check": "status_code",
"assert": "equals",
"expect": 200,
"msg": "assert response status code"
},
{
"check": "headers.\"Content-Type\"",
"assert": "equals",
"expect": "application/json; charset=utf-8",
"msg": "assert response header Content-Type"
},
{
"check": "body.url",
"assert": "equals",
"expect": "https://postman-echo.com/get?foo1=bar1&foo2=bar2",
"msg": "assert response body url"
}
]
}

22
examples/api/get.yml Normal file
View File

@@ -0,0 +1,22 @@
name: ""
request:
method: GET
url: /get
params:
foo1: bar1
foo2: bar2
headers:
Postman-Token: ea19464c-ddd4-4724-abe9-5e2b254c2723
validate:
- check: status_code
assert: equals
expect: 200
msg: assert response status code
- check: headers."Content-Type"
assert: equals
expect: application/json; charset=utf-8
msg: assert response header Content-Type
- check: body.url
assert: equals
expect: https://postman-echo.com/get?foo1=bar1&foo2=bar2
msg: assert response body url

45
examples/api/post.json Normal file
View File

@@ -0,0 +1,45 @@
{
"name": "",
"request": {
"method": "POST",
"url": "/post",
"headers": {
"Content-Length": "58",
"Content-Type": "text/plain",
"Postman-Token": "$session_token"
},
"body": "This is expected to be sent back as part of response body."
},
"validate": [
{
"check": "status_code",
"assert": "equals",
"expect": 200,
"msg": "assert response status code"
},
{
"check": "headers.\"Content-Type\"",
"assert": "equals",
"expect": "application/json; charset=utf-8",
"msg": "assert response header Content-Type"
},
{
"check": "body.data",
"assert": "equals",
"expect": "This is expected to be sent back as part of response body.",
"msg": "assert response body data"
},
{
"check": "body.json",
"assert": "equals",
"expect": null,
"msg": "assert response body json"
},
{
"check": "body.url",
"assert": "equals",
"expect": "https://postman-echo.com/post",
"msg": "assert response body url"
}
]
}

30
examples/api/post.yml Normal file
View File

@@ -0,0 +1,30 @@
name: ""
request:
method: POST
url: /post
headers:
Content-Length: "58"
Content-Type: text/plain
Postman-Token: $session_token
body: This is expected to be sent back as part of response body.
validate:
- check: status_code
assert: equals
expect: 200
msg: assert response status code
- check: headers."Content-Type"
assert: equals
expect: application/json; charset=utf-8
msg: assert response header Content-Type
- check: body.data
assert: equals
expect: This is expected to be sent back as part of response body.
msg: assert response body data
- check: body.json
assert: equals
expect: null
msg: assert response body json
- check: body.url
assert: equals
expect: https://postman-echo.com/post
msg: assert response body url

45
examples/api/put.json Normal file
View File

@@ -0,0 +1,45 @@
{
"name": "",
"request": {
"method": "PUT",
"url": "/put",
"headers": {
"Content-Length": "58",
"Content-Type": "text/plain",
"Postman-Token": "5d357b2b-0f10-4ded-bc9a-299ebef7a2d5"
},
"body": "This is expected to be sent back as part of response body."
},
"validate": [
{
"check": "status_code",
"assert": "equals",
"expect": 200,
"msg": "assert response status code"
},
{
"check": "headers.\"Content-Type\"",
"assert": "equals",
"expect": "application/json; charset=utf-8",
"msg": "assert response header Content-Type"
},
{
"check": "body.data",
"assert": "equals",
"expect": "This is expected to be sent back as part of response body.",
"msg": "assert response body data"
},
{
"check": "body.json",
"assert": "equals",
"expect": null,
"msg": "assert response body json"
},
{
"check": "body.url",
"assert": "equals",
"expect": "https://postman-echo.com/put",
"msg": "assert response body url"
}
]
}

30
examples/api/put.yml Normal file
View File

@@ -0,0 +1,30 @@
name: ""
request:
method: PUT
url: /put
headers:
Content-Length: "58"
Content-Type: text/plain
Postman-Token: 5d357b2b-0f10-4ded-bc9a-299ebef7a2d5
body: This is expected to be sent back as part of response body.
validate:
- check: status_code
assert: equals
expect: 200
msg: assert response status code
- check: headers."Content-Type"
assert: equals
expect: application/json; charset=utf-8
msg: assert response header Content-Type
- check: body.data
assert: equals
expect: This is expected to be sent back as part of response body.
msg: assert response body data
- check: body.json
assert: equals
expect: null
msg: assert response body json
- check: body.url
assert: equals
expect: https://postman-echo.com/put
msg: assert response body url

View File

@@ -7,18 +7,18 @@ import (
)
// generated by examples/har/demo.har using HttpRunner v3.1.6
const demoHttpRunnerJSONPath = "demo_httprunner.json"
const demoHttpRunnerYAMLPath = "demo_httprunner.yaml"
var (
demoHttpRunnerJSONPath hrp.TestCasePath = "demo_httprunner.json"
demoHttpRunnerYAMLPath hrp.TestCasePath = "demo_httprunner.yaml"
)
func TestCompatTestCase(t *testing.T) {
testcaseFromJSON := &hrp.TestCasePath{Path: demoHttpRunnerJSONPath}
err := hrp.NewRunner(t).Run(testcaseFromJSON)
err := hrp.NewRunner(t).Run(&demoHttpRunnerJSONPath)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}
testcaseFromYAML := &hrp.TestCasePath{Path: demoHttpRunnerYAMLPath}
err = hrp.NewRunner(t).Run(testcaseFromYAML)
err = hrp.NewRunner(t).Run(&demoHttpRunnerYAMLPath)
if err != nil {
t.Fatalf("run testcase error: %v", err)
}

View File

@@ -585,13 +585,13 @@
{
"check": "status_code",
"assert": "equals",
"expect": 302,
"expect": 200,
"msg": "assert response status code"
},
{
"check": "headers.\"Content-Type\"",
"assert": "equals",
"expect": "text/plain; charset=utf-8",
"expect": "application/json; charset=utf-8",
"msg": "assert response header Content-Type"
}
]
@@ -695,13 +695,13 @@
{
"check": "status_code",
"assert": "equals",
"expect": 302,
"expect": 200,
"msg": "assert response status code"
},
{
"check": "headers.\"Content-Type\"",
"assert": "equals",
"expect": "text/plain; charset=utf-8",
"expect": "application/json; charset=utf-8",
"msg": "assert response header Content-Type"
}
]

View File

@@ -411,11 +411,11 @@ teststeps:
validate:
- check: status_code
assert: equals
expect: 302
expect: 200
msg: assert response status code
- check: headers."Content-Type"
assert: equals
expect: text/plain; charset=utf-8
expect: application/json; charset=utf-8
msg: assert response header Content-Type
- name: ""
request:
@@ -490,11 +490,11 @@ teststeps:
validate:
- check: status_code
assert: equals
expect: 302
expect: 200
msg: assert response status code
- check: headers."Content-Type"
assert: equals
expect: text/plain; charset=utf-8
expect: application/json; charset=utf-8
msg: assert response header Content-Type
- name: ""
request:

View File

@@ -0,0 +1,78 @@
{
"config": {
"name": "api test demo",
"variables": {
"user_agent": "iOS/10.3",
"device_sn": "TESTCASE_SETUP_XXX",
"os_platform": "ios",
"app_version": "2.8.6"
},
"base_url": "https://postman-echo.com",
"herader": [
{
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Host": "postman-echo.com",
"User-Agent": "PostmanRuntime/7.28.4"
}
],
"verify": false,
"export": [
"session_token"
]
},
"teststeps": [
{
"name": "test api /get",
"api": "examples/api/get.json",
"variables": {
"user_agent": "iOS/10.4",
"device_sn": "$device_sn",
"os_platform": "ios",
"app_version": "2.8.7"
},
"extract": {
"session_token": "body.headers.\"postman-token\""
}
},
{
"name": "test api /post",
"api": "examples/api/post.json",
"variables": {
"user_agent": "iOS/10.5",
"device_sn": "$device_sn",
"os_platform": "ios",
"app_version": "2.8.9"
},
"validate": [
{
"eq": [
"status_code",
200
]
},
{
"eq": [
"body.headers.postman-token",
"ea19464c-ddd4-4724-abe9-5e2b254c2723"
]
}
]
},
{
"name": "test api /put",
"api": "examples/api/put.json",
"variables": {
"user_agent": "iOS/10.6",
"device_sn": "$device_sn",
"os_platform": "ios",
"app_version": "2.8.10"
},
"extract": {
"session_token": "body.headers.\"postman-token\""
}
}
]
}

View File

@@ -0,0 +1,47 @@
config:
name: 'api test demo'
variables:
user_agent: iOS/10.3
device_sn: TESTCASE_SETUP_XXX
os_platform: ios
app_version: 2.8.6
base_url: 'https://postman-echo.com'
herader:
- Accept: '*/*'
Accept-Encoding: 'gzip, deflate, br'
Cache-Control: no-cache
Connection: keep-alive
Host: postman-echo.com
User-Agent: PostmanRuntime/7.28.4
verify: false
export:
- session_token
teststeps:
- name: 'test api /get'
api: examples/api/get.json
variables:
user_agent: iOS/10.4
device_sn: $device_sn
os_platform: ios
app_version: 2.8.7
extract:
session_token: 'body.headers."postman-token"'
- name: 'test api /post'
api: examples/api/post.json
variables:
user_agent: iOS/10.5
device_sn: $device_sn
os_platform: ios
app_version: 2.8.9
validate:
- { eq: [ status_code, 200 ] }
- { eq: [ body.headers.postman-token, ea19464c-ddd4-4724-abe9-5e2b254c2723 ] }
- name: 'test api /put'
api: examples/api/put.json
variables:
user_agent: iOS/10.6
device_sn: $device_sn
os_platform: ios
app_version: 2.8.10
extract:
session_token: 'body.headers."postman-token"'

View File

@@ -0,0 +1,18 @@
{
"config": {
"name": "reference testcase test",
"base_url": "https://postman-echo.com",
"variables": {
"os_platform": "ios"
}
},
"teststeps": [
{
"name": "run demo_httprunner.json",
"testcase": "examples/demo_httprunner.json",
"variables": {
"os_platform": "$os_platform"
}
}
]
}

View File

@@ -0,0 +1,11 @@
config:
name: "reference testcase test"
base_url: "https://postman-echo.com"
variables:
os_platform: 'ios'
teststeps:
- name: run demo_httprunner.yaml
testcase: examples/demo_httprunner.yaml
variables:
os_platform: $os_platform