pytest框架(四)

摘要:
测试用例设置和拆卸代码示例1#coding=utf-82importpytest345defsetup_Module():6print(“setup_Module:整个.py模块只执行一次”)7print(例如,在所有用例开始之前,浏览器只打开一次”

测试用例setup和teardown

pytest框架(四)第1张

代码示例一

1 #coding=utf-8
2 importpytest
3 
4 
5 defsetup_module():
6     print("setup_module:整个.py模块只执行一次")
7     print("比如:所有用例开始前只打开一次浏览器")
8 
9 
10 defteardown_module():
11     print("teardown_module:整个.py模块只执行一次")
12     print("比如:所有用例结束只最后关闭浏览器")
13 
14 
15 defsetup_function():
16     print("setup_function:每个用例开始前都会执行")
17 
18 
19 defteardown_function():
20     print("teardown_function:每个用例结束前都会执行")
21 
22 
23 deftest_one():
24     print("正在执行----test_one")
25     x = "this"
26     assert 'h' inx
27 
28 
29 deftest_two():
30     print("正在执行----test_two")
31     x = "hello"
32     assert hasattr(x, 'check')
33 
34 
35 deftest_three():
36     print("正在执行----test_three")
37     a = "hello"
38     b = "hello world"
39     assert a inb
40 
41 
42 if __name__ == "__main__":
43     pytest.main(["-s", "test_fixt.py"])

代码示例二

1 #coding=utf-8
2 
3 importpytest
4 
5 
6 classTestClass:
7     defsetup(self):
8         print("setup: 每个用例开始前执行")
9 
10     defteardown(self):
11         print("teardown: 每个用例结束后执行")
12 
13     defsetup_class(self):
14         print("setup_class:所有用例执行之前")
15 
16     defteardown_class(self):
17         print("teardown_class:所有用例执行之前")
18 
19     defsetup_method(self):
20         print("setup_method:  每个用例开始前执行")
21 
22     defteardown_method(self):
23         print("teardown_method:  每个用例结束后执行")
24 
25     deftest_one(self):
26         x = "this"
27         assert 'h' inx
28 
29     deftest_two(self):
30         x = "hello"
31         assert hasattr(x, 'check')
32 
33     deftest_three(self):
34         a = "hello"
35         b = "hello world"
36         assert a inb
37 
38 
39 if __name__ == "__main__":
40     pytest.main(['-q', 'test_class.py'])

免责声明:文章转载自《pytest框架(四)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ROS惯导数据发布(Python)容器编排系统K8s之包管理器Helm基础使用(一)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

pytest文档70-Hook钩子函数完整API总结

前言 pytest 的钩子函数有很多,通过钩子函数的学习可以了解到pytest在执行用例的每个阶段做什么事情,也方便后续对pytest二次开发学习。 详细文档可以查看pytest官方文档https://docs.pytest.org/en/latest/reference.html#hooks 钩子函数总结 第一部分:setuptools 引导挂钩要求足够...

使用IDEA写Python之pytest环境搭建及第一个程序编写

一、准备篇 Python环境:3.8.3 开发工具:IDEA,对你没有看错 二、IDEA下安装开发环境 1. python的下载 https://www.python.org/downloads/ PS:关于Python的环境搭建,此处略 2. idea下python插件的安装 点击File->Settings...->Plugins,点击ma...

pytest自学第二期

好,现在开始学习pytest第二章2 pytest初级用法2.1 通过python解释器调用 pytest 在控制台输入命令:   python -m pytest 文档说这种方式相当于直接调用 pytest,唯一的不同是这样调用会把当前目录添加到sys.path (这句是重点,重点是啥,我不知道,   我不知道这有什么用c 以后就这样,如果有自己学过但是...

pytest文档40-pytest.ini配置用例查找规则(面试题)

前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例。 pytest.ini 配置文件可以修改用例的匹配规则。 pytest命令行参数 cmd打开输入pytest -h 查看命令行参数找到 [pytest] ini-options python_files (args) 匹配 python 用例文件, 如tes...

allure安装与环境变量配置

                                                                         allure安装与环境变量配置 一、下载allure文件    1、下载地址:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-comman...

Python自动化之pytest框架使用详解

pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点: 简单灵活,容易上手 支持参数化 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests) pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(...