Pytest-1

作者: iscona | 来源:发表于2023-08-06 15:47 被阅读0次

    1.安装

    pip install -U pytest
    #查看是否安装成功
    pytest --version
    

    2.新建一个test_case.py文件

    文件命名以test_开头或者_test结尾
    测试函数以test_开头
    断言使用assert
    

    3.文件中写测试代码

    # content of test_sample.py
    def func(x):
        return x +1
    
    def test_answer():
        assert func(3)==5
    

    4.执行测试用例

    1.pytest
    2.py.test
    3.python -m pytest
    #如果指定运行单个文件,使用 -q参数
    #不带参数,在某个文件夹下执行时,它会查找该文件夹下所有的符合条件的用例(查看用例设计原则)
    
    ➜  testcases pytest
    ============================= test session starts ==============================
    platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
    rootdir: /Users/qina/workspace/python-space/python-test/testcases
    collected 1 item
    
    test_api.py F                                                            [100%]
    
    =================================== FAILURES ===================================
    _________________________________ test_answer __________________________________
    
        def test_answer():
    >       assert  func(3)==5
    E       assert 4 == 5
    E        +  where 4 = func(3)
    
    test_api.py:8: AssertionError
    =========================== short test summary info ============================
    FAILED test_api.py::test_answer - assert 4 == 5
    ============================== 1 failed in 0.06s ===============================
    
    ➜  testcases pytest -q test_api.py
    F                                                                        [100%]
    =================================== FAILURES ===================================
    _________________________________ test_answer __________________________________
    
        def test_answer():
    >       assert  func(3)==5
    E       assert 4 == 5
    E        +  where 4 = func(3)
    
    test_api.py:8: AssertionError
    =========================== short test summary info ============================
    FAILED test_api.py::test_answer - assert 4 == 5
    1 failed in 0.07s
    

    相关文章

      网友评论

          本文标题:Pytest-1

          本文链接:https://www.haomeiwen.com/subject/pkcwpdtx.html