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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值