关于python:pytest使用ABC

2次阅读

共计 2238 个字符,预计需要花费 6 分钟才能阅读完成。

pytest 是什么?

pytest 是一个 python 的单元测试框架。能够用来为函数、类等写单元测试文件。

疾速开始

装置:pip install pytest

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


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

执行:

$ pytest

后果:

=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 1 item

test_sample.py F                                                     [100%]

================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================

留神:测试文件和测试函数都应该以 test_ 结尾。

FAQ

1. 如何只对一部分函数进行测试?

两种办法:

办法一:应用 pytest.mark

通过 mark 对所有的函数进行标记,而后在运行测试命令的时候指定应该运行哪些。

# test_with_mark.py
@pytest.mark.finished
def test_func1():
assert 1 == 1

@pytest.mark.unfinished
def test_func2():
 assert 1 != 1

运行:

$ pytest -m finished tests/test_with_mark.py`

如此则实现了只对做了 finished 标记的函数进行单元测试。

办法二(举荐):应用 pytest.mark.skip

在不想做测试的函数上应用 pytest.mark.skip 标记,以跳过测试。

 # test_skip.py
      
@pytest.mark.skip(reason='out-of-date api')
def test_connect():
pass

运行:$ pytest tests/test_skip.py

Pytest 还反对应用 pytest.mark.skipif 为测试函数指定被疏忽的条件。

@pytest.mark.skipif(conn.__version__ < '0.2.0',
                          reason='not supported until v0.2.0')
def test_api():
pass

2. 如何给测试函数批量传递参数?

状况 1:传递 一个参数

# test_parametrize.py

@pytest.mark.parametrize('passwd',
                      ['123456',
                       'abcdefdfs',
                       'as52345fasdf4'])
def test_passwd_length(passwd):
    assert len(passwd) >= 8

状况 2:传递多个参数

# test_parametrize.py

@pytest.mark.parametrize('user, passwd',
                         [('jack', 'abcdefgh'),
                          ('tom', 'a123456a')])
def test_passwd_md5(user, passwd):
    db = {
        'jack': 'e8dc4081b13434b45189a720b77b6818',
        'tom': '1702a132e769a623c1adb78353fc9503'
    }

    import hashlib

    assert hashlib.md5(passwd.encode()).hexdigest() == db[user]

应用 -v 执行测试:

$ pytest -v tests/test-function/test_parametrize.py::test_passwd_md5

3. 如果函数应用数据库,如何解决数据库的连贯和敞开?

可在固件中解决数据库的连贯和敞开。

   # test_db.py
   
   @pytest.fixture()
   def db():
       print('Connection successful')
   
       yield
   
       print('Connection closed')
   
   
   def search_user(user_id):
       d = {'001': 'xiaoming'}
       return d[user_id]
   
   
   def test_search(db):
       assert search_user('001') == 'xiaoming'

如果想更细的跟踪固件执行,能够应用 --setup-show 选项

$ pytest --setup-show tests/fixture/test_db.py
正文完
 0