Pytest-2

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

    1.测试方法

    # content of test_sample.py
    def func(x):
        return x +1
    
    def test_answer():
        assert func(3)==5
    
    ➜  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
    

    2.测试类

    #!/usr/bin/env python
    # -*- coding:utf8 -*-
    
    class TestClass:
        def test_one(self):
            x = "this"
            assert 'h' in x
    
        def test_two(self):
            x = "hello"
            assert hasattr(x, 'check')
    
    ➜  testcases pytest -q test_api.py
    .F                                                                                 [100%]
    ======================================== FAILURES ========================================
    ___________________________________ TestClass.test_two ___________________________________
    
    self = <testcases.test_api.TestClass object at 0x10d0d35f8>
    
        def test_two(self):
            x = "hello"
    >       assert hasattr(x, 'check')
    E       AssertionError: assert False
    E        +  where False = hasattr('hello', 'check')
    
    test_api.py:11: AssertionError
    ================================ short test summary info =================================
    FAILED test_api.py::TestClass::test_two - AssertionError: assert False
    1 failed, 1 passed in 0.06s
    
    

    相关文章

      网友评论

          本文标题:Pytest-2

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