Pytest-2

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容