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

乙醇 创建于 over 6 years 之前

最后更新: about 1 month 之前

阅读数: 1

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

背景

接上一节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 =================================================================
0

相关课程

webium简明教程
图文
webium简明教程

课程分类: 测试框架

开箱即用的page object模式

  • 已完结
  • 已更新8集
  • 最后更新时间: 2024-03-18 12:48:12

免费

查看详情
TestNG教程
图文
TestNG教程

课程分类: 测试框架

Java语言中最流行的测试框架了

  • 已完结
  • 已更新12集
  • 最后更新时间: 2024-03-18 12:55:14

免费

查看详情
python unittest测试框架教程
图文
python unittest测试框架教程

课程分类: 测试框架

python 自带的单元测试框架

  • 已完结
  • 已更新8集
  • 最后更新时间: 2024-03-18 12:12:46

免费

查看详情
TDD测试驱动开发教程
图文
TDD测试驱动开发教程

课程分类: 测试框架 软件测试基础

TDD其实并不神秘

  • 已完结
  • 已更新7集
  • 最后更新时间: 2024-03-18 11:53:22

免费

查看详情