代码示例一
1 # coding=utf-8 2 3 def func(x): 4 return x + 1 5 6 7 def test_answer(): 8 assert func(3) == 5
运行结果
E:pyYouYoupytest_demo>pytest test_sample.py ============================= test session starts ============================= platform win32 -- Python 3.6.1, pytest-3.7.2, py-1.5.4, pluggy-0.7.1 rootdir: E:pyYouYoupytest_demo, inifile: collected 1 item test_sample.py F [100%] ================================== FAILURES =================================== _________________________________ test_answer _________________________________ def test_answer(): > assert func(3)==5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:7: AssertionError
示例代码二
1 # coding=utf-8 2 3 class TestClass: 4 def test_one(self): 5 x = "this" 6 assert 'h' in x 7 8 def test_two(self): 9 x = "hello" 10 assert hasattr(x, 'check')
运行结果
E:pyYouYoupytest_demo>py.test -q test_class.py .F [100%] ================================== FAILURES =================================== _____________________________ TestClass.test_two ______________________________ self = <pyYouYou.pytest_demo.test_class.TestClass object at 0x0000000002C319E8> def test_two(self): x = "hello" > assert hasattr(x, 'check') E AssertionError: assert False E + where False = hasattr('hello', 'check') test_class.py:10: AssertionError 1 failed, 1 passed in 0.10 seconds