pytest框架結構運行規則及命名方式


Pytest框架結構

 

Import pytest
類似的setup,teardown同樣更靈活,還有個session()

 

模塊級 (setup_module/teardown_module) 不在類中的函數有用

函數級 (setup_function/teardown_function) 不在類中的函數有用

類級 (setup_class/teardown_class)只在 類中前后運行一次。

方法級 (setup_method/teardown_methond) 運行在類中方法始末





import pytest
def setup_module():
print('整個模塊.py開始')

def teardown_module():
print('整個模塊的.py結束')

def setup_function():
print('不在類中的函數前')

def teardown_function():
print('不在類中的函數后')

def test_w_one():
print('不在類中的方法1')

def test_w_two():
print('不在類中的方法2')

class TestClass:
def setup_class(self):
print('類前面')

def teardown_class(self):
print('類之后')

def setup_method(self):
print('方法前')

def teardown_method(self):
print('方法后')

def test_one(self):
x='this'
assert 'h' in x

def test_two(self):
x='hello'
assert 'h4'==x

def test_three(self):
a='hello'
b='hello world'
assert a in b

if __name__ == '__main__':
pytest.main("-s -v","pytestDemo.py")




整個模塊.py開始
不在類中的函數前
不在類中的方法1

不在類中的函數后
pytestDemo.py::test_w_one ✓ 20% ██ 不在類中的函數前
不在類中的方法2

不在類中的函數后
pytestDemo.py::test_w_two ✓ 40% ████ 類前面
方法前

方法后
pytestDemo.py::TestClass.test_one ✓ 60% ██████ 方法前


――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― TestClass.test_two ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

self = <pytest_2.pytestDemo.TestClass object at 0x105b0f438>

def test_two(self):
x='hello'
> assert 'h4'==x
E AssertionError: assert 'h4' == 'hello'
E - h4
E + hello

pytestDemo.py:39: AssertionError

方法后
pytestDemo.py::TestClass.test_two ⨯ 80% ████████ 方法前

方法后
類之后
整個模塊的.py結束
pytestDemo.py::TestClass.test_three ✓ 100% ██████████

Results (0.14s):
4 passed
1 failed
- pytestDemo.py:37 TestClass.test_two




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM