从0到1精通自动化测试,pytest自动化测试框架,用例运行规则(二)

112 篇文章 3 订阅
52 篇文章 6 订阅
本文详细介绍了如何从0开始精通自动化测试,重点聚焦在pytest框架上。内容包括用例设计原则,如test开头的文件和函数,Test开头的类;使用pytest -h获取帮助信息;通过python -m执行pytest;执行用例的不同规则,如按目录、文件、关键字、节点运行,以及标记表达式;最后讲解了-x和--maxfail选项在遇到错误时如何控制测试的中断策略。
摘要由CSDN通过智能技术生成

一、用例设计原则

文件名以 test_*.py 文件和 *_test.py
以 test_ 开头的函数
以 Test 开头的类
以 test_ 开头的方法
所有的包 pakege 必须要有 __init__.py 文件

二、help帮助

1.查看pytest命令行参数,可以用pytest -h 或pytest —help查看

C:\Users\admin>pytest -h
usage: pytest [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

general:
  -k EXPRESSION         only run tests which match the given substring
                        expression. An expression is a python evaluatable
                        expression where all names are substring-matched
                        against test names and their parent classes. Example:
                        -k 'test_method or test_other' matches all test
                        functions and classes whose name contains
                        'test_method' or 'test_other', while -k 'not
                        test_method' matches those that don't contain
                        'test_method' in their names. Additionally keywords
                        are matched to classes and functions containing extra
                        names in their 'extra_keyword_matches' set, as well as
                        functions which have names assigned directly to them.
  -m MARKEXPR           only run tests matching given mark expression.
                        example: -m 'mark1 and not mark2'.
  --markers             show markers (builtin, plugin and per-project ones).
  -x, --exitfirst       exit instantly on first error or failed test

reporting:
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  --verbosity=VERBOSE   set verbosity

只贴了一部分

三、按以下目录写用例

D:MOMO\
    __init__.py

    test_class.py
        #  content of  test_class.py  
        class TestClass:
            def test_one(self):
                x = "this"
                assert 'h' in x

            def test_two(self):
                x = "hello"
                assert hasattr(x, 'check')

            def test_three(self):
                a = "hello"
                b = "hello world"
                assert a in b

    test_sample.py
        #  content of  test_sample.py
        def func(x):
            return x +1

        def test_answer():
            assert func(3)==5

四、python -m

cmd执行pytest用例有三种方法,以下三种方法都可以,(一般推荐第一个)

  1. pytest
  2. py.test
  3. python -m pytest

如果不带参数,在某个文件夹下执行时,它会查找该文件夹下所有的符合条件的用例(查看用例设计原则)

五、执行用例规则

1.执行某个目录下所有的用例

pytest 文件名/

2.执行某一个py文件下用例

pytest 脚本名称.py

3.-k 按关键字匹配

pytest -k "MyClass and not method"

这将运行包含与给定字符串表达式匹配的名称的测试,其中包括Python使用文件名,类名和函数名作为变量的运算符。 上面的例子将运行TestMyClass.test_something但不运行TestMyClass.test_method_simple

4.按节点运行

每个收集的测试都分配了一个唯一的nodeid,它由模块文件名和后跟说明符组成
来自参数化的类名,函数名和参数,由:: characters分隔

运行.py模块里面的某个函数

pytest test_mod.py::test_func

运行.py模块里面,测试类里面的某个方法

pytest test_mod.py::TestClass::test_method

5.标记表达式

pytest -m slow

将运行用@ pytest.mark.slow装饰器修饰的所有测试

6.从包里面运行

pytest —pyargs pkg.testing

这将导入pkg.testing并使用其文件系统位置来查找和运行测试

六、-x 遇到错误时停止测试

pytest -x test_class.py

从运行结果可以看出,本来有3个用例,第二个用例失败后就没继续往下执行了

D:\MOMO>pytest -x test_class.py
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO, inifile:
collected 3 items

test_class.py .F

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <MOMO.test_class.TestClass object at 0x0000000003A29780>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:11: AssertionError
===================== 1 failed, 1 passed in 0.05 seconds ======================

七、—maxfail=num

pytest —maxfail=1

当用例错误个数达到指定数量时,停止测试

D:\MOMO>pytest --maxfail=1
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO, inifile:
collected 4 items

test_class.py .F

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <MOMO.test_class.TestClass object at 0x0000000003A3D080>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:11: AssertionError
===================== 1 failed, 1 passed in 0.06 seconds ======================

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值