安裝
pip install pytest
用例設計原則
- 文件名 為test_*.py或*_test.py
- test_* 為測試函數(shù)名
- Test* 為測試類名
- test_* 為測試方法名
用例執(zhí)行
- <font color=red>執(zhí)行目錄下的所有文件</font>
pytest dirname/
pytest test_mod.py
pytest test_mod.py::TestClass::test_method
- 按照關(guān)鍵字執(zhí)行(匹配模塊名、類名、函數(shù)名)
pytest -k "MyClass"
- <font color=red>按照標記執(zhí)行</font>
@pytest.mark.slow
def test_slow():
pass
@pytest.mark.fast
def test_fast():
pass
>>>pytest -m "slow and fast"
@pytest.mark.skip(reason="mark as skip")
def test_skip():
pass
@pytest.mark.skipif(skip_flag=False, reason="skip determined by the flag")
def test_skip_if():
pass
pytest -x
pytest --maxfail=n
- <font color=red>腳本調(diào)試使用</font>
pytest.main()
pytest.main(['test_mod.py', '-v'])
用例編寫
assert a == b
assert a != b
assert a >= b
assert b <= b
assert a in b
assert a not in b
assert a is b
assert a is not b
assert True
assert False
import pytest
class TestClass:
def setup_class(self):
print("setup_class:類中所有用例執(zhí)行之前")
def teardown_class(self):
print("teardown_class:類中所有用例執(zhí)行之前")
def setup_method(self):
print("setup_method: 每個用例開始前執(zhí)行")
def teardown_method(self):
print("teardown_method: 每個用例結(jié)束后執(zhí)行")
def setup(self):
print("setup: 每個用例開始前執(zhí)行")
def teardown(self):
print("teardown: 每個用例結(jié)束后執(zhí)行")
def test_one(self):
print("執(zhí)行第一個用例")
def test_two(self):
print("執(zhí)行第二個用例")
def setup_module():
print("setup_module:整個.py模塊只執(zhí)行一次")
def teardown_module():
print("teardown_module:整個.py模塊只執(zhí)行一次")
def setup_function():
print("setup_function:每個方法用例開始前都會執(zhí)行")
def teardown_function():
print("teardown_function:每個方法用例結(jié)束前都會執(zhí)行")
- <font color=red>參數(shù)化 pytest.mark.parametrize(argnames, args)</font>
@pytest.mark.parametrize('text', ['test1','test2','test3'])
def test_one(text):
print(text)
@pytest.mark.parametrize(['username', 'pwd'], [('a', 1), ('b', 2), ('c', 3)])
def test_two(username, pwd):
print(username, pwd)
Fixture
import pytest
@pytest.fixture()
def get_url():
return "http://www.baidu.com"
def test_web(get_url):
print(get_url)
if __name__ == '__main__':
pytest.main()
- 作用域
- session: 會話級,一次測試只執(zhí)行一次,所有被找到的函數(shù)和方法都可用
- module: 模塊級,每個模塊執(zhí)行一次,模塊內(nèi)函數(shù)和方法都可使用
- function: 函數(shù)級,每個測試函數(shù)都會執(zhí)行一次固件
- class: 類級別,每個測試類執(zhí)行一次,所有方法都可以使用
#./conftest.py
@pytest.fixture(scope="class")
def text():
print("開始執(zhí)行")
yield 'test'
print("執(zhí)行完畢")
#./test_sample.py
@pytest.mark.usefixtures('text')
class TestClass:
def test_one(self):
print(text)
#./conftest.py
@pytest.fixture(scope='module', params=['test1', 'test2'])
def text(request):
print('開始執(zhí)行')
yield request.param
print('執(zhí)行完畢')
#./ test_sample.py
def test_one(text):
print(text)
- 配置文件
- 所有的fixture放在conftest.py文件中,自動發(fā)現(xiàn),統(tǒng)一管理
生成報告
- <font color=red>html報告、順序執(zhí)行、失敗重試</font>
pip install pytest-html # 生成html報告
pip install pytest-ordering # 按照順序執(zhí)行用例
pip install pytest-rerunfailures # 失敗重試
pytest test_mod.py --html=./report.html
# pytest.mark.run(order=1)
# pytest.mark.run(order=2)
pytest test_mod.py # 按照mark標記的order順序執(zhí)行
pytest test_mod.py --reruns 3 # 失敗重試3次
pytest --junitxml=report.xml
# install java
# install allure
pip install allure-pytest
pytest casedir/ --alluredir ./report
allure serve report