微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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=[插件名])

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐