pytest 使用

运行方式

  • python 模块名.py 添加pytest_main()
  • python -m python

pytest会进行自动查找默认查找

  1. 文件 test_*.py 和 *_test.py 开头或者结尾
  2. 模块需要以test开头

自定义查找规则
在运行的根目录下,创建pytest.ini文件

[pytest]
python_files =
    test_*.py
    check_*.py
    example_*.py
python_functions = *_test
python_classes = *Suite

以上配置文件表示,pytest查找模块名为test_,check_,example_开头的模块,函数名为_test结尾的文件,Suite结尾的类

参数

1.指定名称或目录进行测试

  • 指定测试模块: pytest 模块名.py
  • 指定测试目录:pytest 测试用例路径/

2.通过节点id进行测试

节点id的组成:

  1. py模块名::类名::方法名
  2. py模块名::函数名

例:

pytest test_add.py::TestDemo::test_add_1

3.-k关键字匹配

pytest -k "test_add and not test_add_1"

运行test_add 不运行 test_add_1

4.-m标记用例
使用mark标记测试用例

标记测试用例步骤

  1. pytest.ini文件注册标签
  2. 标签贴到指定的测试用例或者类 @pytest.mark.标签名称
  3. pytest -m “标签名称”(加上双引号)

标记测试用例方法
1.类标记
方法一:使用 @pytest.mark.名称 进行标记

 @pytest.mark.error
class TestLogin(unittest.TestCase):
    def test_login_1(self):
        self.assertEqual(1,1)
    def test_login_2(self):
        self.assertEqual(1,2)

方法二:使用类属性 pytestmark = [pytest.mark.名称,pytest.mark.名称]

class TestLogin(unittest.TestCase):
    pytestmark = [pytest.mark.login,pytest.mark.error]
    def test_login_1(self):
        self.assertEqual(1,1)
    def test_login_2(self):
        self.assertEqual(1,2)

2.方法标记
使用标签 @pytest.mark.名称

class TestDemo(unittest.TestCase):
    @pytest.mark.success
    def test_add_1(self):
        self.assertEqual(1,1)
    @pytest.mark.login
    def test_add_2(self):
        self.assertEqual(1,2)

3.跳过标记
@pytest.mark.skip(“跳过理由”)
@pytest.mark.skipif(“条件”)
例如:满足系统是windows时跳过

@pytest.mark.skipif(sys.platform=="win32")

执行顺序

pytest的执行顺序安装方法写的前后顺序决定
unnittest的执行顺序由函数名的ASCII码决定

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code-Study

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值