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