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

背景

接上一节v2ex网站的查看论坛节点信息的api。

我们在上一节的测试用例里只断言了返回值的name字段必须与我们传入的入参相同,但是返回值的id却没有进行判定。这一节里我们加强一下测试脚本,实现判断id的功能。

测试数据

python, id=90
java, id=63
nodejs, id=436
go, id=375

代码实现

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

class TestV2exApiWithExpectation(object):
domain = 'https://www.v2ex.com/' @pytest.mark.parametrize('name,node_id', [('python', 90), ('java', 63), ('go', 375), ('nodejs', 436)]) def test_node(self, name, node_id):
path = 'api/nodes/show.json?name=%s' %(name)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == name
assert res['id'] == node_id
assert 0

运行及结果

$ 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 9 items v2ex_api_test.py .FFFFFFFF ============================================================================= FAILURES ==============================================================================
______________________________________________________________ TestV2exApiWithParams.test_node[python] ______________________________________________________________ self = <v2ex_api_test.TestV2exApiWithParams object at 0x10618eb10>, 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 0x106691790>, 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 0x10666dc50>, 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 0x106691890>, 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
__________________________________________________________ TestV2exApiWithExpectation.test_node[python-90] __________________________________________________________ self = <v2ex_api_test.TestV2exApiWithExpectation object at 0x1066d20d0>, name = 'python', node_id = 90 @pytest.mark.parametrize('name,node_id', [('python', 90), ('java', 63), ('go', 375), ('nodejs', 436)]) def test_node(self, name, node_id):
path = 'api/nodes/show.json?name=%s' %(name)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == name
assert res['id'] == node_id
> assert 0
E assert 0 v2ex_api_test.py:40: AssertionError
___________________________________________________________ TestV2exApiWithExpectation.test_node[java-63] ___________________________________________________________ self = <v2ex_api_test.TestV2exApiWithExpectation object at 0x1066e9690>, name = 'java', node_id = 63 @pytest.mark.parametrize('name,node_id', [('python', 90), ('java', 63), ('go', 375), ('nodejs', 436)]) def test_node(self, name, node_id):
path = 'api/nodes/show.json?name=%s' %(name)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == name
assert res['id'] == node_id
> assert 0
E assert 0 v2ex_api_test.py:40: AssertionError
___________________________________________________________ TestV2exApiWithExpectation.test_node[go-375] ____________________________________________________________ self = <v2ex_api_test.TestV2exApiWithExpectation object at 0x10666d790>, name = 'go', node_id = 375 @pytest.mark.parametrize('name,node_id', [('python', 90), ('java', 63), ('go', 375), ('nodejs', 436)]) def test_node(self, name, node_id):
path = 'api/nodes/show.json?name=%s' %(name)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == name
assert res['id'] == node_id
> assert 0
E assert 0 v2ex_api_test.py:40: AssertionError
_________________________________________________________ TestV2exApiWithExpectation.test_node[nodejs-436] __________________________________________________________ self = <v2ex_api_test.TestV2exApiWithExpectation object at 0x1066d2710>, name = 'nodejs', node_id = 436 @pytest.mark.parametrize('name,node_id', [('python', 90), ('java', 63), ('go', 375), ('nodejs', 436)]) def test_node(self, name, node_id):
path = 'api/nodes/show.json?name=%s' %(name)
url = self.domain + path
res = requests.get(url).json()
assert res['name'] == name
assert res['id'] == node_id
> assert 0
E assert 0 v2ex_api_test.py:40: AssertionError
================================================================ 8 failed, 1 passed in 1.81 seconds ================================================================

pytest.10.使用fixture参数化测试预期结果的更多相关文章

  1. pytest.9.使用fixture参数化接口入参

    From: http://www.testclass.net/pytest/test_api_using_params/ 背景 接上一节v2ex网站的查看论坛节点信息的api.具体如下: 节点信息 获 ...

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

  3. Pytest 学习(二十五)- 解决pytest参数化测试标题都一样问题

    前言 使用参数化测试化后,allure的报告如下显示: 源代码如下: # -*- coding: utf-8 -*- # @Time : 2020/12/13 17:27 # @Author : lo ...

  4. 【pytest】(十)fixture参数化-巧用params和ids优雅的创建测试数据

    我们都知道参数化. 比如我要测试一个查询接口/test/get_goods_list,这个接口可以查询到商品的信息. 在请求中,我可以根据请参数goods_status的不同传值,可以查询到对应状态的 ...

  5. testng入门教程10 TestNG参数化测试

    在TestNG的另一个有趣的功能是参数测试.在大多数情况下,你会遇到这样一个场景,业务逻辑需要一个巨大的不同数量的测试.参数测试,允许开发人员运行同样的测试,一遍又一遍使用不同的值. TestNG让你 ...

  6. Python 中如何实现参数化测试?

    Python 中如何实现参数化测试? 之前,我曾转过一个单元测试框架系列的文章,里面介绍了 unittest.nose/nose2 与 pytest 这三个最受人欢迎的 Python 测试框架. 本文 ...

  7. Pytest【定制fixture】

    在pytest中的fixture是在测试函数运行前后,由pytest执行的外壳函数,fixture中的代码可以定制,满足多变的测试需求:包括定义传入测试中的数据集.配置测试前系统的初始化状态.为批量测 ...

  8. Google单元测试框架gtest之官方sample笔记3--值参数化测试

    1.7 sample7--接口测试 值参数不限定类型,也可以是类的引用,这就可以实现对类接口的测试,一个基类可以有多个继承类,那么可以测试不同的子类功能,但是只需要写一个测试用例,然后使用参数列表实现 ...

  9. pytest进阶之fixture函数

    fixture函数存在意义 与python自带的unitest测试框架中的setup.teardown类似,pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.但是相 ...

随机推荐

  1. Pycharm出现Segmentation fault...(interrupted by signal 11: SIGSEGV)的解决方法

    众所周知,用pycharm远程服务器debug代码已经成为学习深度学习相关代码的有力工具,但是最近创建了一个虚拟环境,进行debug的时候,莫名会出现下面这个错误,看的我都抽风了 bash: line ...

  2. pandas apply()函数参数 args

    #!/usr/bin/python import pandas as pd data = {'year':[2000,2001,2002,2001,2002],'value':[1.5,1.7,3.6 ...

  3. 如何让jpa 持久化时不校验指定字段

    源文:https://www.toocruel.net/jpa-validate/ 怎么让jpa 持久化时不校验指定字段 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥 ...

  4. Java中的Arrays类使用详解

    首先先创建一个打印数组的方法,方便后面直接使用 public static void output(int []a) { for(int i=0;i<a.length;i++) { System ...

  5. seo:网站地图提交

    对于SEO,网站地图的好处就更多了: 1.为搜索引擎蜘蛛提供可以浏览整个网站的链接简单的体现出网站的整体框架出来给搜索引擎看: 2.为搜索引擎蜘蛛提供一些链接,指向动态页面或者采用其他方法比较难以到达 ...

  6. 大整数四则运算------(c++ 实现 乘法没有用傅里叶变换)

    /* 优点: 1 支持负整数的运算 2 良好的输出形式 没有前导零 3 支持cin直接输入 支持cout直接输出 4 支持整数的直接赋值 big_int x=100; 缺点: 1 封装不好 基本都是友 ...

  7. <<操作,&0xff以及|的巧妙运用(以POJ3523---The Morning after Halloween(UVa 1601)为例)

    <<表示左移,如a<<1表示将a的二进制左移一位,加一个0,&0xff表示取最后8个字节,如a&0xff表示取a表示的二进制中最后8个数字组成一个新的二进制数, ...

  8. Linux系统修改Home下的目录为英文

    修改Home下的目录为英文 修改目录映射文件名: vim .config/user-dirs.dirs 修改如下:XDG_DESKTOP_DIR="$HOME/Desktop"XD ...

  9. 洛谷P1337 【[JSOI2004]平衡点 / 吊打XXX】(模拟退火)

    洛谷题目传送门 很可惜,充满Mo力的Mo拟退火并不是正解.不过这是一道最适合开始入手Mo拟退火的好题. 对模拟退火还不是很清楚的可以看一下 这道题还真和能量有点关系.达到平衡稳态的时候,物体的总能量应 ...

  10. OutputStream 和 Writer

    OutputStream类(直接操作byte数组) 该类是字节输出流的抽象类,定义了输出流的各种操作方法.如下图是OutputStream的层次结构: ByteArrayOutputStream:字节 ...