遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(02):用例查找规则

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

用例查找规则

pytest命令方式运行时,用例查找规则如下:

命令 说明
pytest(等价于:python -m pytest)

运行当前目录及子目录下所有用例

pytest ./ 运行当前目录及子目录下所有用例
pytest case\sub_case\ 运行指定目录及子目录下所有用例
 pytest case\test_qzcsbj.py 指定模块运行
pytest -k test_2 按关键字(函数/方法名)匹配运行
pytest case\test_qzcsbj.py::test_a 指定函数运行
pytest case\test_qzcsbj.py::TestQzcsbj1 指定类运行
pytest case\test_qzcsbj.py::TestQzcsbj1::test_c 指定类方法运行

 

演示

数据

 

test_qzcsbj.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
import pytest
 
def inc(x):
    return x + 1
 
def test_a():
    print("---test_a")
    assert inc(0) == 1
 
class TestQzcsbj1:
    def test_b(self):
        print("---test_b")
        assert "qz" in "qzcsbj"
 
    def test_c(self):
        print("---test_c")
        assert "cs" in "qzcsbj"
class TestQzcsbj2:
    def test_d(self):
        print("---test_d")
        assert "bj" in "qzcsbj"

  

test_qzcsbj1.py

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
def test_1():
    print("---test_1")
    assert 1 == 1

  

test_qzcsbj2.py

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
import pytest
 
def test_2():
    print("---test_2")
    assert 1 == 1

 

演示 

说明:下面命令都统一加上了-vs参数

pytest
运行了当前目录及子目录下所有用例

 

pytest ./

运行了当前目录及子目录下所有用例

 

pytest case\sub_case\

运行指定目录及子目录下所有用例

 

pytest case\test_qzcsbj.py

指定模块运行

 

pytest -k test_2

按关键字(函数/方法名)匹配运行

 

pytest case\test_qzcsbj.py::test_a

指定函数运行

 

pytest case\test_qzcsbj.py::TestQzcsbj1

指定类运行

 

pytest case\test_qzcsbj.py::TestQzcsbj1::test_c

指定类方法运行

 

补充:pycharm中运行方法、类、模块、包

  • 方法和类,直接点击绿色按钮运行
  • 模块和包,选中模块或者包,然后右键运行

 

posted @   全栈测试笔记  阅读(865)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2022-02-22 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示