From: http://www.testclass.net/pytest/test_api_using_params/

背景

接上一节v2ex网站的查看论坛节点信息的api。具体如下:

节点信息

获得指定节点的名字,简介,URL 及头像图片的地址。

https://www.v2ex.com/api/nodes/show.json

Method: GET
Authentication: None
接受参数: name: 节点名(V2EX 的节点名全是半角英文或者数字)
例如: https://www.v2ex.com/api/nodes/show.json?name=python

我们试一下,通过传入不同的name,我们可以获取不同的节点信息。上面例子里我们获取了python讨论区的信息。现在我们把name改成java,该接口会返回java讨论区节点的信息,如下所示

https://www.v2ex.com/api/nodes/show.json?name=python

{
"id" : 63,
"name" : "java",
"url" : "http://www.v2ex.com/go/java",
"title" : "Java",
"title_alternative" : "Java",
"topics" : 1219,
"stars" : 1547, "header" : "The most popular programming language.", "footer" : null, "created" : 1272669207,
"avatar_mini" : "//v2ex.assets.uxengine.net/navatar/03af/dbd6/63_mini.png?m=1509589840",
"avatar_normal" : "//v2ex.assets.uxengine.net/navatar/03af/dbd6/63_normal.png?m=1509589840",
"avatar_large" : "//v2ex.assets.uxengine.net/navatar/03af/dbd6/63_large.png?m=1509589840"
}

那么新的需求来了,现在我们要测试给定的几个节点名称(python/java/go/nodejs),v2ex的节点api可以正确返回节点的名字

需求分析

根据3A原则,我们可以设计如下的用例

代码

v2ex_api_test.py的文件中添加如下内容

import requests
import pytest class TestV2exApiWithParams(object):
domain = 'https://www.v2ex.com/' @pytest.fixture(params=['python', 'java', 'go', 'nodejs'])
def lang(self, request):
return request.param def test_node(self, lang):
path = 'api/nodes/show.json?name=%s' %(lang)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == lang
assert 0

需要注意的点

  • 每次都可以用request.param来访问本次传入fixture中的参数
  • 在测试方法中传入同名的fixture方法名可以直接访问fixture
  • 使用assert(0)强制用例失败,这样可以看到每次fixture的参数值

运行

$ pytest v2ex_api_test.py
======================================================================== test session starts ========================================================================
platform darwin -- Python 2.7.12, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /Users/easonhan/code/testclass.net/src/pytest, inifile:
collected 5 items v2ex_api_test.py .FFFF ============================================================================= FAILURES ==============================================================================
______________________________________________________________ TestV2exApiWithParams.test_node[python] ______________________________________________________________ self = <v2ex_api_test.TestV2exApiWithParams object at 0x105e0edd0>, lang = 'python' def test_node(self, lang):
path = 'api/nodes/show.json?name=%s' %(lang)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == lang
> assert 0
E assert 0 v2ex_api_test.py:27: AssertionError
_______________________________________________________________ TestV2exApiWithParams.test_node[java] _______________________________________________________________ self = <v2ex_api_test.TestV2exApiWithParams object at 0x1075e2750>, lang = 'java' def test_node(self, lang):
path = 'api/nodes/show.json?name=%s' %(lang)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == lang
> assert 0
E assert 0 v2ex_api_test.py:27: AssertionError
________________________________________________________________ TestV2exApiWithParams.test_node[go] ________________________________________________________________ self = <v2ex_api_test.TestV2exApiWithParams object at 0x107636190>, lang = 'go' def test_node(self, lang):
path = 'api/nodes/show.json?name=%s' %(lang)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == lang
> assert 0
E assert 0 v2ex_api_test.py:27: AssertionError
______________________________________________________________ TestV2exApiWithParams.test_node[nodejs] ______________________________________________________________ self = <v2ex_api_test.TestV2exApiWithParams object at 0x1075e2790>, lang = 'nodejs' def test_node(self, lang):
path = 'api/nodes/show.json?name=%s' %(lang)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == lang
> assert 0
E assert 0 v2ex_api_test.py:27: AssertionError
================================================================ 4 failed, 1 passed in 1.91 seconds =================================================================

用例执行失败,但是每次运行时lang的值我们可以看的很明白。

pytest.9.使用fixture参数化接口入参的更多相关文章

  1. Spring AOP 自定义注解获取http接口及WebService接口入参和出参

    注解方法实现过程中可以采用如下获取方式:—以下为例  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHo ...

  2. springboot项目中接口入参的简单校验

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  3. java接口入参模板化,适用于企业化服务远程调度模板化的场景,接口入参实现高度可配置化

    需求:远程服务接口模板化配置提供接入服务 模板接口分为三个模块:功能路由.参数校验.模板入库 路由:这里的实现方式很简单,就是根据业务标识找到对应的处理方法 参数校验: 参数校验这步涉及模板和校验类两 ...

  4. pytest.10.使用fixture参数化测试预期结果

    From: http://www.testclass.net/pytest/test_api_with_expected_result/ 背景 接上一节v2ex网站的查看论坛节点信息的api. 我们在 ...

  5. (一)将mockjs集成到VUE中后,怎样根据接口入参返回mock结果

    1)安装mockjs,这一步跳过 2)在项目中建立mock模块,笔者的目录结构如下 mock模块与接口模块一一对应,有一个接口,就有一个mock 3)编写登陆模块mock接口,代码如下: import ...

  6. robot:接口入参为图片时如何发送请求

    https://www.cnblogs.com/changyou615/p/8776507.html 接口是上传图片,通过F12抓包获得如下信息 由于使用的是RequestsLibrary,所以先看一 ...

  7. RobotFramework:发现一个大坑,当post接口入参为json时,千万不能用sojson转化后的json串(ride解析会有异常,非sojson工具问题),直接用浏览器粘过来的就行

    问题背景: 和以往一样愉快的进行着自动化测试,突然就不停的提示我,“程序异常”,查看log发现data中的json变为了数组?????? 那算了,我不先组装入参数据直接data=json入参吧,wha ...

  8. pytest框架 里 fixture 参数化的方法

  9. Postman—上个接口返回数据作为下个接口入参

    //将数据解析成json格式 var data=JSON.parse(responseBody); //获取totalRentPrice值 var totalRentPrice=jsonData.da ...

随机推荐

  1. JavaBasic_02

    Java的基础框架 3W:What How Why What:一个东西是什么,具备什么样的功能 怎么用 How: 怎么做?功能如何实现 读源代码(jdk)->学习很多,优雅的编程技巧建立在wha ...

  2. [LeetCode&Python] Problem 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  3. CSS使用方法

    CSS行内样式: 在开始标签内添加style样式属性 如:<p style="color:red;">内容</p> CSS内部样式: 内部样式(嵌入样式), ...

  4. CodeForces - 441E:Valera and Number (DP&数学期望&二进制)

    Valera is a coder. Recently he wrote a funny program. The pseudo code for this program is given belo ...

  5. BFS广度优先搜索 炸弹人

    题面:一个人在一个坐标放炸弹,请问可以可以杀死的敌人数目最大是,并且输出该点的坐标 G代表敌人 .代表该位置可以走 "#"代表该位置存在障碍物 并且防止炸弹的蔓13 13 3 3 ...

  6. spring管理

    spring管理 SqlMapConfig.xml: <?xml version="1.0" encoding="UTF-8"?> <bean ...

  7. (9)模板层 - templates(模板语言、语法、取值、过滤器、变量的使用)

    django的模板语言:DTL 模板语言的变量传入 这个是标签 {{ 变量名 }} {{ 变量名 }}   #模板语言的替换可以在模板中的任意位置生效 PS:通过 . 可以做深度查询 模板语言的过滤器 ...

  8. LeetCode - Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  9. Cassandra Demo--Python操作cassandra

    ================================================================ 创建keyspace和table CREATE KEYSPACE ex ...

  10. python-廖雪峰,map/reduce学习笔记

    # _*_ coding:utf-8 _*_from functools import reduce def str2int(s): digits = {'0': 0, '1': 1, '2': 2, ...