pytest-入门

2021-05-31  本文已影响0人  Chaweys

知识点:
1、cmd命令行模式下:
如果只执行 pytest ,会查找当前目录及其子目录下以  test_*.py  或 *_test.py 文件,找到文件后,在文件中找到以  test 开头函数并执行;
如果只想执行某个文件,可以 pytest start.py 
加上-q,就是显示简单的结果: pytest -q start.py 

2、用Pytest写用例时候,一定要按照下面的规则去写,否则不符合规则的测试用例是不会执行的
文件名以 test_*.py 文件和 *_test.py
以test_ 开头的函数
以Test 开头的类,不能包含 __init__ 方法
以test_ 开头的类里面的方法
所有的包 pakege 必须要有__init__.py 文件 {如果是要用cmd命令行执行pytest运行所有的用例文件}


#coding=utf-8

def func(x):
    return x + 1


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

class TestStart:
    def test_one(self):
        assert 'h' in 'hello'

    def test_two(self):
        assert 3 == 2

CMD命令运行结果:
E:\ProjectStudy\Pytest框架>pytest -q test_start.p
或者:
E:\ProjectStudy\Pytest框架>pytest 

.F                                                                                                               [100%]
====================================================== FAILURES =======================================================
_________________________________________________ TestStart.test_two __________________________________________________

self = <start.TestStart object at 0x00000204769A1A60>

    def test_two(self):
>       assert 3 == 2
E       assert 3 == 2

start.py:8: AssertionError
=============================================== short test summary info ===============================================
FAILED start.py::TestStart::test_two - assert 3 == 2
1 failed, 1 passed in 0.11s


3、cmd命令行模式执行用例的方式:
(1)、执行当前目录下的所有test_ 用例文件
pytest                   
   
(2)、执行当前目录下指定的test_ 用例文件 
pytest 脚本名称.py          


(3)、
pytest test_start.py::TestStart            执行test_start.py模块里的某个类
pytest test_start.py::TestStart::test_one  执行test_start.py模块里的某个类里的某个函数
pytest test_start.py::test_answer          执行test_start.py模块里的某个函数

(4)、-m标记表达式,将运行用 @pytest.mark.login 装饰器修饰的所有测试   
pytest -m login 

(5)、-q简单打印,只打印测试用例的执行结果
pytest -q test_start.py  

(6)、-s详细打印 
pytest -s test_start.py  

(7)、-x遇到错误时停止测试
pytest -x test_start.py  

(8)、--maxfail=num当错误数达到num时停止测试
pytest test_start.py --maxfail=1  

(9)、-k http 执行匹配用例名称中含有http的 
pytest -s -k http test_start.py 

(10)、-k "not http"根据用例名称排除某些用例 
pytest  -k "not http" test_start.py  

(11)、-k "method or weibo"同时匹配不同的用例名称
pytest -s -k "method or weibo" test_start.py   
上一篇 下一篇

猜你喜欢

热点阅读