用例管理
关键概念:
测试用例(testcase)应该是完整且独立的,每条测试用例应该是都可以独立运行的。
测试用例(测试场景)是测试步骤(teststep)的有序、集合,每一个测试步骤对应一个 API 的请求描述。
测试用例集(testsuite)是测试用例的 无序 集合,集合中的测试用例应该都是相互独立,不存在先后依赖关系的;如果确实存在先后依赖关系,那就需要在测试用例中完成依赖的处理。
多个测试场景组成一个测试套件,运行一个测试套件,可以同时执行多个测试场景。
接口定义(api definition):为了更好地对接口描述进行管理,使用独立的文件对接口描述进行存储,即每个文件对应一个接口描述。
V2.0
建议大家用最新版本的,但是有兴趣了解httprunner的架构变化的同学,可以继续读V2.0模块的内容。
V2.0的版本中,存在api definition的概念。
·所有的api都置于api文件夹,每个api创建一个yml/json文件进行管理
·testcase中的teststep引用对应的api
优点是:api管理方便;缺点是:在testcase中的teststep可以直接引用api definition,不管testcase是单个步骤的简单场景还是多个步骤的复杂场景,都需要进行引用api definition。这样的话,对于单个步骤的简单场景而言,又和api definition很相似,这样就会造成重复描述,而且容易混淆。
那么,我们如何在testcase>teststep中引用api呢?如何引用testcase呢?
V2.0案例学习
在teststep中可以通过api字段引用api definition 、通过testcase字段引用testcase。下面我们根据脚手架快速创建一个项目httprunner --startproject hruntest2.0(需要确保安装的是httpruner3.0以下的版本)来说明:
$ httprunner --startproject hruntest2.0 && tree hruntest2.0
Start to create new project: hruntest2.0
CWD: /Users/boyizhang/PycharmProjects/apitest
created folder: hruntest2.0
created folder: hruntest2.0/api
created folder: hruntest2.0/testcases
created folder: hruntest2.0/testsuites
created folder: hruntest2.0/reports
created file: hruntest2.0/api/demo_api.yml
created file: hruntest2.0/testcases/demo_testcase.yml
created file: hruntest2.0/testsuites/demo_testsuite.yml
created file: hruntest2.0/debugtalk.py
created file: hruntest2.0/.env
created file: hruntest2.0/.gitignore
hruntest2.0
├── api
│ └── demo_api.yml
├── debugtalk.py
├── reports
├── testcases
│ └── demo_testcase.yml
└── testsuites
└── demo_testsuite.yml
api definition
# api/demo_api.yml
name: demo api
variables:
var1: value1
var2: value2
request:
url: /api/path/$var1
method: POST
headers:
Content-Type: "application/json"
json:
key: $var2
validate:
- eq: ["status_code", 200]
testcase
config:
name: "demo testcase"
variables:
device_sn: "ABC"
username: ${ENV(USERNAME)}
password: ${ENV(PASSWORD)}
base_url: "http://127.0.0.1:5000"
teststeps:
-
name: demo step 1
api: path/to/api1.yml
variables:
user_agent: 'iOS/10.3'
device_sn: $device_sn
extract:
- token: content.token
validate:
- eq: ["status_code", 200]
在teststep中使用api选项来引用api definition,执行testcase/demo_testcase.yml用例文件。(注:api definition不属于testcase范畴,无法直接执行api/下的yml/json文件)。在teststep中传的字段的优先级会比api definition下的高,也就是说,如果teststep传某个字段,那么会有限使用teststep传的那个,如果没有,则再使用api definition的字段。
V3.0
为了简单,在HttpRunner v2.x中的API概念已经被取消了。可以将API定义为只有一个请求步骤的测试用例(需要重点注意一下这里)。
去掉api的概念,把api的概念转为测试用例的概念。将API定义为只有一个请求步骤的测试用例。以统一的概念:测试用例,使得维护用例的工作更加方便。
按照V2.0的逻辑,需要维护api definition,也需要维护只有一个测试步骤的testcase,而V3.0中,我们只需要维护testcase即可。v2->v3的转变,使得可以在避免重复描述的同时,解决测试用例的依赖关系,从而保证每个测试用例都是独立可运行的。
那么,问题来了,我们应该如何在某个testcase的teststep中引用其他testcase呢?
v3.0案例学习
在测试步骤(teststep)中,可通过 testcase 字段引用其它测试用例,引用方式为对应测试用例文件的路径,绝对路径或相对路径均可。推荐使用相对路径,路径基准为项目根目录,即 debugtalk.py 所在的目录路径。
通过httprunner startproject hruntest3.0 快速创建项目。
$ tree hruntest3.0
hruntest3.0
├── debugtalk.py
├── har
├── reports
└── testcases
├── demo_testcase_ref.yml
└── demo_testcase_request.yml
# demo_testcase_request.yml
config:
name: "request methods testcase with functions"
variables:
foo1: config_bar1
foo2: config_bar2
expect_foo1: config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
export: ["foo3"]
teststeps:
-
name: post form data
variables:
foo2: bar23
request:
method: POST
url: /post
headers:
User-Agent: HttpRunner/${get_httprunner_version()}
Content-Type: "application/x-www-form-urlencoded"
data: "foo1=$foo1&foo2=$foo2&foo3=$foo3"
validate:
- eq: ["status_code", 200]
- eq: ["body.form.foo1", "$expect_foo1"]
- eq: ["body.form.foo2", "bar23"]
- eq: ["body.form.foo3", "bar21"]
# demo_testcase_ref.yml
config:
name: "request methods testcase: reference testcase"
variables:
foo1: testsuite_config_bar1
expect_foo1: testsuite_config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
teststeps:
-
name: request with functions
variables:
foo1: testcase_ref_bar1
expect_foo1: testcase_ref_bar1
testcase: testcases/demo_testcase_request.yml
export:
- foo3
-
name: post form data
variables:
foo1: bar1
request:
method: POST
url: /post
headers:
User-Agent: HttpRunner/${get_httprunner_version()}
Content-Type: "application/x-www-form-urlencoded"
data: "foo1=$foo1&foo2=$foo3"
validate:
- eq: ["status_code", 200]
- eq: ["body.form.foo1", "bar1"]
- eq: ["body.form.foo2", "bar21"]
在teststep中使用testcase字段来引用,执行demo_testcase_ref.yml用例文件,当执行到name = request with functions这个步骤的时候会先执行其引用的testcase:testcases/demo_testcase_request.yml。 在根目录下,执行hrun testcases/demo_testcase_ref.yml,可以看到,程序也生成了该case:testcases/demo_testcase_request.yml的测试文件:
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理