之前一直使用unittest
进行Python
的单元测试,最近接触pytest
文档并使用它进行目前Python
项目的单元测试
编写用例
使用pytest
可以方便的编写出测试用例,只需要在函数用例或者类用例前加test_
或Test
前缀即可
函数用例
1 | def test_create_get_category_api(): |
类用例
1 | class TestGw(object): |
测试用例
测试单个函数
1 | pytest .\qst\tests\api\zcy\test_factory_category.py::test_create_get_category_attrs_api |
测试单个类
1 | pytest .\qst\tests\business\test_gw.py::TestGw |
测试单个类的某个方法
1 | pytest .\qst\tests\business\test_gw.py::TestGw::test_create_goods |
测试文件所有用例
1 | pytest .\qst\tests\api\zcy\test_factory_category.py |
测试文件夹下所有用例
1 | pytest .\qst\tests |
mock的使用
1 | def test_create_goods(self, mocker): |