微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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



版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐