序文
HttpRunnerでは、支持変数宣言(変数)及び参照($ VAR)のメカニズム。設定では、キー変数は変数で定義することができ、テスト、そしてテスト工程は、変数$ +変数名によって参照することができます。
現在の試験の範囲に定義されたテスト変数(TestStep)のステップに制限され、差は、すべての場所のグローバル変数を定義した設定で、全体のテスト(テストケース)を参照することができる、ということです
ローカル変数
ケースのログインには、口座番号とパスワードは、ユースケースを書くときに、これは、単一のパラメータ変数を記述するために最善になることがあり、通常は、ハードコードされています。テストデータとその後のメンテナンスのためのコードの分離を達成するために。
我々はテストで変数を宣言すると、範囲は現在のテストでのみ有効です。変数宣言variables
など、変数およびキーと値のペアの対応する値を、
- test:
name: login case1
variables:
user: test
psw: 123456
参考ユーザーとPSWの変数は$user
、$psw
次のスクリプトtest_var.ymlを完了します
- config:
name: logincase
variables: {}
- test:
name: login case1
variables:
user: test
psw: 123456
request:
url: http://127.0.0.1:8000/api/v1/login/
method: POST
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: $user
password: $psw
extract:
- token: content.token # 提取token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.msg, login success!]
- eq: [content.code, 0]
試験本実施形態の開始とローカル変数は、他の場合には効果がテストを使用しません
ユースケースを実行します
営業成績をhrun使用
D:\soft\untitled>hrun test_var.yml
login case1
INFO POST http://127.0.0.1:8000/api/v1/login/
INFO status_code: 200, response_time(ms): 384.72 ms, response_length: 109 bytes
INFO start to extract from response object.
INFO start to validate.
.
----------------------------------------------------------------------
Ran 1 test in 0.394s
OK
INFO Start to render Html report ...
INFO Generated Html report: D:\soft\untitled\reports\1569114664.html
グローバル変数
あなたはグローバル変数を設定したい場合は、変数を宣言する必要があります(変数)はconfigの下に入れ、効果にファイル全体となるよう.yml
- config:
name: logincase
variables:
user: test
psw: 123456
- test:
name: login case1
request:
url: http://127.0.0.1:8000/api/v1/login/
method: POST
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: $user
password: $psw
extract:
- token: content.token # 提取token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.msg, login success!]
- eq: [content.code, 0]
業績
D:\soft\untitled>hrun test_config_var.yml
login case1
INFO POST http://127.0.0.1:8000/api/v1/login/
INFO status_code: 200, response_time(ms): 580.17 ms, response_length: 109 bytes
INFO start to extract from response object.
INFO start to validate.
.
----------------------------------------------------------------------
Ran 1 test in 0.584s
OK
INFO Start to render Html report ...
INFO Generated Html report: D:\soft\untitled\reports\1569114978.html