Python测试框架pytest(02)PyCharm设置运行pytest、pytest.main()

29 篇文章 8 订阅
28 篇文章 13 订阅

目录

1、PyCharm设置运行pytest

2、pytest.main()

2.1、带参数运行

2.2、运行指定用例

2.3、加载指定插件


1、PyCharm设置运行pytest

打开PyCharm,依次打开Preferences--->Tools--->Python Integrated Tools,将Testing里的Default test runner选择项选为pytest,保存即可。

右键运行,可以看到以pytest去运行。

2、pytest.main()

main 函数有2个可选参数:

args:命令行参数列表。

plugins:初始化期间要自动注册的插件对象列表。

pytest.main() 不带任何参数时与在命令行直接运行 pytest 命令一样,默认运行的是当前目录及子目录的所有文件夹的测试用例。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

pytest.main()

2.1、带参数运行

1、在命令行运行pytest -s

在pytest.main()里面等同于

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

pytest.main(["-s"])

2、在命令行运行pytest -s -x

在pytest.main()里面等同于

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

pytest.main(["-s", "-x"])

2.2、运行指定用例

1、命令行跳转到项目根目录,执行test/case文件夹下的全部用例

pytest test/case

在pytest.main()里面等同于

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

pytest.main(["test/case"])

2、命令行跳转到项目根目录,执行test/case/test_case1.py文件里的全部用例

pytest test/case/test_case1.py

在pytest.main()里面等同于

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

pytest.main(["test/case/test_case1.py"])

3、命令行跳转到项目根目录,执行test/case/test_case1.py文件里的test_login用例

pytest test/case/test_case1.py::test_login

在pytest.main()里面等同于

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

pytest.main(["test/case/test_case1.py::test_login"])

2.3、加载指定插件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

pytest.main(["test/case"], plugins=[插件名])

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wangmcn

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

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

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

打赏作者

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

抵扣说明:

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

余额充值