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

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 ================================================================

原文地址:https://www.cnblogs.com/Raul2018/p/9472322.html

时间: 2024-07-30 19:19:11

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

JUnit实战(1) - JUnit起步(Parameterized参数化测试)

创建Java Project项目,项目名称:ch01-jumpstart Calculator.java public class Calculator { public double add(double number1, double number2) { return number1 + number2; } } CalculatorTest.java import static org.junit.Assert.*; import junit.framework.Assert; impo

JUnit之参数化测试和成组测试的使用

JUnit之参数化测试和成组测试的简单使用 参数化测试 正如数组替代int a0,a1,a2一样,测试加法时assertEquals(3.0, h.add(1, 2), 0.1);相当于声明一个变量,要测试100个怎么办. 所以,有了参数化测试,使用一个Collection收集所有的数据--加法时每一次测试需要的几个数据构成一组,n个组构成Collection. 然后按照JUnit的用法要求,写出单元测试类.(偷懒一下,不想写被测试的业务类X了.下面的例子中假设要测试的方法是,判断一个数是否奇数

Junit 4 Tutorials(Junit 4 教程) 四、Junit4 参数化测试

Junit 4 参数化测试 允许通过变化范围的参数值来测试方法.参数擦测试可以通过以下简单的步骤实现: 对测试类添加注解 @RunWith(Parameterized.class) 将需要使用变化范围参数值测试的参数定义为私有变量 使用上一步骤声明的私有变量作为入参,创建构造函数 .创建一个使用@Parameters注解的公共静态方法,它将需要测试的各种变量值通过集合的形式返回. 使用定义的私有变量定义测试方法 Junit 4 参数化测试样例 EvenNumberChecker.java 校验输

TestNG的参数化测试、共享线程池配置、参数默认值配置

在使用TestNG进行测试时,经常会使用到一些参数化配置,比如数据库.连接池.线程池数, 使用TestNG的参数@Parameter注解进行自动化读取 使用多线程的方式运行测试代码配置: 在'<suite>'标签中配置data-provider-thread-count="20" Java代码: /** * * <p> * Title: TestngParameters * </p> * * <p> * 参考配置文件testng-para

【t1】&#183;badboy跟jmeter结合的网页参数化测试(pigeon详细说)

[163网易邮箱网页的参数化测试],在网上查找的资料中学了好久,发现对于我这种小白来说,还是不够详细,我自己思考细想了很久才慢慢有点掌握,所以把参数化的详细过程梳理一遍,帮自己记录,也帮一下小白学习. [1]第一步,肯定是先将两个软件工具下载下来,两个工具打包放在百度云了,可直接使用http://pan.baidu.com/s/1micsNt2.当然你要自己官网下载也行呵呵.jmeter解压后可直接使用,badboy压缩包解压好是BadboyInstaller-2.0.5.exe,运行该文件进行

junit 单元测试 - 参数化测试

junit4.x版本需要引入如下jar包: hamcrest-core-1.3.jar junit-4.12-beta-3.jar 新建一个计算器类,如下: package com.pt; public class Calculate { /** * 加法 * @param d1 * @param d2 * @return */ public static double add(double d1,double d2){ return d1 + d2; } /** * 减法 * @param d

JUnit4学习笔记(二):参数化测试与假定(Assumption)

一.一个简单的测试 编写一个只有一种运算的计算器: 1 public class Calculator { 2 public static double divide(int dividend, int divisor) { 3 return dividend / divisor; 4 } 5 } 为这个方法编写测试: 1 public class CalculatorTest { 2 //允许误差 3 private static final double DELTA = 0.01; 4 5

Junit4使用(并使用参数化测试)

如果是基于Eclipse使用Junit,在现在的Eclipse版本中都已经集成了Junit,可以根据自身的需求自由使用Junit3或者Junit4.在本文中主要通过测试简单算术程序(加,减,乘,除)来介绍Junit4的使用,并引入一个简单的案例进行讲解. Step 1: 写出基本的算术代码Calculate.java package com.ysc.main; public class Calculate { public static int add(int a, int b) { retur

使用JUnit测试预期异常

开发人员常常使用单元测试来验证的一段儿代码的操作,很多时候单元测试可以检查抛出预期异常( expected exceptions)的代码.在Java语言中,JUnit是一套标准的单元测试方案,它提供了很多验证抛出的异常的机制.本文就探讨一下他们的优点. 我们拿下面的代码作为例子,写一个测试,确保canVote() 方法返回true或者false, 同时你也能写一个测试用来验证这个方法抛出的IllegalArgumentException异常. (Guava类库中提供了一个作参数检查的工具类--P