pytest和unittest的比较
上一篇 /
下一篇 2017-08-03 16:06:27
/ 个人分类:Pytest
1.unittest的固定格式要遵循,固定格式是创建 test class,继承unittest.testcase; pytest没有固定格式,只要有def test_**就可以是一个测试用例。也就是如果不需要的话可以不用创建TEST CLASS.#coding=utf-8#功能deffunc(x):returnx + 1#测试用例deftest_answer():assertfunc(3) == 5
2.pytest的fixture比unittest的Setup/teardown更灵活。更自由的定义fixture,任何函数加上@pytest.fixture的装饰器就成了fixture,然后通过fixture装载(3种装载方法)在测试用例中装载fixture。而且,fixture的使用范围可以是function,module,class,session. unittest也可以定义fixture使用范围,用法是将setup/teardown封装到一个class中,然后在其他Test class中继承这个封装好的class.
import pytest
@pytest.fixture()
def before():
num = 10
print ('\nbefore each test')
return num
def test_1(before):
assert before == 10
print ('test_1()')
def test_2(before):
assert before == 1
print ('test_2()')
3.pytest使用python原生的assert断言,表达形式更多
def f():
return 3
def test_function():
assert f() == 4
4.基于第二点,pytest基本上就万能了,什么读json文件作为参数,做法:def load_file(json_file) return data. 将load_file加上fixture标签
收藏
举报
TAG: