文章目录
1.简介:
Pytest是python的一种单元测试框架。
1. pytest 特点
- 入门简单,文档丰富
- 支持单元测试,功能测试
- 支持参数化,重复执行,部分执行,测试跳过
- 兼容其他测试框架(nose,unittest 等)
- 支持生成html报告
- 可集成CI环境(Jenkins 等)
- 第三方插件丰富,良好的自定义扩展性
2. pytest VS unittest
unittest
- 测试文件必须先 import unittest
- 测试类必须继承unittest.TestCase
- 测试方法必须以“test_”开头
- 测试类必须要有unittest.main()方法
- unittest只有setup/teardown装载测试用例
pytest
- 测试文件名必须以“test_”开头
- 测试类以Test开头,并且不能带有 init 方法
- 测试方法必须以“test_”开头
- 除了有setup/teardown,还能更自由的定义fixture装载测试用例
2.Pytest运行
pytest运行规则:
- 查找当前目录及其子目录下以test_.py或_test.py文件
- 找到文件后,在文件中找到以test开头函数并执行
1.方式一:文件里面的函数直接查找并运行
# testSample.py文件
def test_one():
x = "this"
assert 'h' in x
def test_two():
x = "hello"
assert hasattr(x, 'check') # 判断某个对象是否包含某个属性
在pycharm的Terminal工具里面,直接运行pytest -q testSample.py 即可
2.方式二:文件里类里面的测试方法
# testSample.py文件
def test_one():
x = "this"
assert 'h' in x
def test_two():
x = "hello"
assert hasattr(x, 'check')
class TestClass(object):
def test_three(self):
y = 2
assert y == 2
还是方法一对应的文件,添加一个测试类和测试方法,直接在Terminal工具执行pytest -q testSample.py,发现仍然可以运行TestClass类里面的方法,第二个测试用例失败,其他2个都通过了。
3.通过pycharm运行测试用例
因为pycharm默认的测试框架是unittest,需要修改运行时的测试框架,方法如下:
- 第一步:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test
- 第二步:修改一下代码,如下所示
import pytest
def test_one():
x = "this"
assert 'h' in x
def test_two():
x = "hello"
assert hasattr(x, 'check')
class TestClass(object):
def test_three(self):
y = 2
assert y == 2
if __name__ == '__main__':
pytest.main('-q testSamply.py')
- 第三步:右键选择pytest运行或者直接运行.py文件
3.setup & teardown
模块级(setup_module/teardown_module)开始于模块始末(文件级别始末)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中的函数)
方法级(setup/teardown)开始于方法始末(在类中的函数,setup_method/teardown_method之后,方法之前,一般使用setup/teardown即可)
函数级(setup_function/teardown_function)只对函数用例生效(不在类中的函数)
import pytest
# 模块中的方法
def setup_module():
print("setup_module:--------------整个.py模块")
print("\n")
def teardown_module():
print("teardown_module:--------------整个.py模块")
def setup_function():
print("setup_function:-----类外每个函数始末")
def teardown_function():
print("teardown_function:-----类外每个函数始末")
# 测试模块中的用例1
def test_one():
print("----test_one-----start--------")
x = "this"
assert 'h' in x
# 测试类
class TestCase():
def setup_class(self):
print("\n")
print("setup_class:first step for every class")
def teardown_class(self):
print("teardown_class:end step for every class")
print("\n")
def setup(self):
print("setup:--------------every test case in class")
def teardown(self):
print("teardown:--------------every test case in class")
def test_three(self):
print("----test_three-----start--------")
x = "this"
assert 'h' in x
def test_four(self):
print("----test_four-----start--------")
x = "this"
assert 's' in x
if __name__ == "__main__":
pytest.main(["-s", "testSample.py"])
运行结果:
testSample.py setup_module:--------------整个.py模块
setup_function:-----类外每个函数始末
.----test_one-----start--------
teardown_function:-----类外每个函数始末
setup_class:first step for every class # 测试类开始前
setup:--------------every test case in class
.----test_three-----start--------
teardown:--------------every test case in class
setup:--------------every test case in class
.----test_four-----start--------
teardown:--------------every test case in class
teardown_class:end step for every class # 测试类开始后
teardown_module:--------------整个.py模块
import pytest
# 测试类
class TestCase():
def setup_class(self):
print("setup_class:first step for every class")
print("\n")
def teardown_class(self):
print("\n")
print("teardown_class:end step for every class")
def setup(self):
print("setup:--------------every test case in class")
def setup_method(self):
print("setup_method:--------------every test case in class")
def teardown(self):
print("teardown:--------------every test case in class")
def teardown_method(self):
print("teardown_method:--------------every test case in class")
def test_three(self):
print("----test_three-----start--------")
x = "this"
assert 'h' in x
def test_four(self):
print("----test_four-----start--------")
x = "this"
assert 's' in x
if __name__ == "__main__":
pytest.main(["-s", "testSample.py"])
运行结果:
testSample.py setup_class:first step for every class
setup_method:--------------every test case in class
setup:--------------every test case in class
.----test_three-----start--------
teardown:--------------every test case in class
teardown_method:--------------every test case in class
setup_method:--------------every test case in class
setup:--------------every test case in class
.----test_four-----start--------
teardown:--------------every test case in class
teardown_method:--------------every test case in class
teardown_class:end step for every class
通过如上结果可看出:
- setup_method & teardown_method 以及 setup & teardown,都是在类里面的方法执行前后执行的
- setup_method & teardown_method要更早一点执行