Pytest脚本中运行用例方式

脚本树如下:

test1文件下test_01.py存放test1和test2用例

test1文件下test_02.py存放test1和test2用例

test2文件下test_03.py存放test1和test2用例

test2文件下test_04.py存放test1和test2用例

1、运行所有用例
import pytest
if __name__ == "__main__":
    pytest.main(['-s',''])


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 8 items

test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.
test1\test_02.py test_02-test1:5
.test_02-test2:5
.
test2\test_03.py test_03-test1:5
.test_04-test2:5
.
test2\test_04.py test04-test1:5
.test04-test2:5
.

============================== 8 passed in 0.25s ==============================

Process finished with exit code 0
--------------------------------------------------------------------------------------

2、运行指定文件夹下用例
import pytest
if __name__ == "__main__":
    pytest.main(['-s','test1'])

"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 4 items

test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.
test1\test_02.py test_02-test1:5
.test_02-test2:5
.

============================== 4 passed in 0.12s ==============================

Process finished with exit code 0

-----------------------------------------------------------------------------------

3、运行指定py文件下用例
import pytest
if __name__ == "__main__":
    pytest.main(['-s','test1/test_01.py'])

"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items

test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.

============================== 2 passed in 0.04s ==============================

Process finished with exit code 0


4、运行指定类下的用例
import pytest
if __name__ == "__main__":
    pytest.main(['-s','test1/test_01.py::Test'])


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items

test1\test_01.py 调用了获取token
test_01-test1:5
.test_01-test2:5
.

============================== 2 passed in 0.04s ==============================

Process finished with exit code 0

-------------------------------------------------------------------------------------


5、运行执行方法下的用例
import pytest
if __name__ == "__main__":
    pytest.main(['-s','test1/test_01.py::Test::test1'])

"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/PytestAutomation/testcase/run_all_test.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\PytestAutomation\testcase
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 1 item

test1\test_01.py 调用了获取token
test_01-test1:5
.

============================== 1 passed in 0.04s ==============================

Process finished with exit code 0

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在编写pytest脚本时,可以通过封装函数和类的方式来提高代码的可重用性和可维护性。下面是一些封装pytest脚本的方法: 1. 函数封装:将一组相关的测试步骤封装到一个函数。这样可以将相似的测试逻辑封装起来,提高代码的复用性。例如: ```python import pytest def test_login(): # 登录步骤 def test_logout(): # 登出步骤 def test_search(): # 搜索步骤 ``` 在上述示例,每个函数代表一个独立的测试用例,其包含了特定的测试步骤。 2. 类封装:根据测试场景的复杂度,可以使用类来封装一组相关的测试用例。这样可以更好地组织和管理测试代码。例如: ```python import pytest class TestLogin: def test_valid_login(self): # 有效登录步骤 def test_invalid_login(self): # 无效登录步骤 class TestSearch: def test_basic_search(self): # 基本搜索步骤 def test_advanced_search(self): # 高级搜索步骤 ``` 在上述示例,每个类代表一个测试场景,其包含了多个相关的测试用例。 3. 重用函数和类:如果有一些通用的测试逻辑需要在多个测试用例使用,可以将其封装为独立的函数或类,并在需要时进行调用。这样可以减少重复代码,提高代码的可维护性。 ```python import pytest def login(username, password): # 登录步骤 def test_valid_login(): login("valid_username", "valid_password") # 其他测试步骤 def test_invalid_login(): login("invalid_username", "invalid_password") # 其他测试步骤 ``` 在上述示例,`login()`函数封装了登录的测试步骤,并在多个测试用例进行调用。 通过函数和类的封装,可以将测试代码组织得更好,提高代码的可读性、可重用性和可维护性。希望这些提示对你有帮助!如有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王大力测试进阶之路

打赏博主喝瓶水吧!!!

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

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

打赏作者

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

抵扣说明:

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

余额充值