Pytest作为一个Python测试框架,不仅简单灵活,新手也可以快速入门,而且具有很多的第三方插件,功能十分强大。因此,Pytest可以说是测试行业从业者必学的工具。本文将为大家介绍Pytest的安装、基本操作、运行时设置,以及参数化,下面一起来拿看看Pytest快速入门使用说明手册吧!
1、安装
(1)全局安装
使用pip 进行安装
pip install -U pytest
检查安装版本
$ pip install -U pytest
This is pytest version 4.4.0, imported from xxxx
(2)项目目录下安装
如果只是将pytest安装到当前项目中,不与其它的版本混淆,使用 virtualenv进行安装
mkdir pytest_project
cd pytest_project
virtualenv .venv
这将会在项目目录下创建pytest-env目录存放虚拟环境。
激活虚拟环境
source .venv/bin/activate
再次使用pip进行安装,安装文件将存放在当前项目目录下,而不再是全局环境变量下
$ pip install pytest
(3)区别
全局安装方式适合所有项目,在项目目录下虚拟化安装只适合当前项目。
2、基本操作
(1)主要内容
基本使用方式:我们将从一个简单的测试开始,Pytest命名规范文件名以test_开头或以_test.py结尾。首先创建一个名为test_capitalize.py的文件,在此文件中编写一个名为capital_case的函数,以字符串作为参数,将参数转化为大写返回。另外编写一个test_capital_case参数函数,主要验证capital_case函数完成它所说的内容,我们用test_作为测试函数名称的前缀。
# test_capitalize.py
def capital_case(x):
return x.capitalize()
def test_capital_case():
assert capital_case('semaphore') == 'Semaphore'
在命令行运行 pytest , 将自动执行 test_capitalize.py 文件中的 test_capital_case 测试方法;
collected 1 item
test_capitalize.py . [100%]
========================================= 1 passed in 0.01 seconds ==========================================
测试用例执行通过。
3、Pytest 运行时设置
(1)xunit 格式
函数级别设置运行时
setup_module
setup
teardown
teardown_module
如下代码
def setup_module():
print("module --setup-- ")
def teardown_module():
print('module --teardown--')
def setup():
print("function --setup--")
def teardown():
print("function --teardown--")
def test_01():
print("---test01---")def test_02(): print('-----test02------')
运行文件 pytest -s -v tmp.py
testcases/tmp.py::test_01
module --setup--
function --setup
-----test01---
PASSED function --teardown--
testcases/tmp.py::test_02
function --setup--
-----test02------
PASSED
function --teardown--
module --teardown--
Class 类级别
tmp2.py
class TestTmp2:
@classmethod
def setup_class(cls):
print('- class setup -')
@classmethod
def teardown_class(cls):
print('- class teardown - ')
def setup(self):
print('- method setup -')
def teardown(self):
print("- method teardown -")
def test_01(self):
print("-- test01 --")
def test_02(self):
print('-- test02 --')
pytest -s -v tmp2.py
tmp2.py::TestTmp2::test_01 - class setup
-- method setup -
-- test01 --
PASSED- method teardown -
testcases/tmp/tmp2.py::TestTmp2::test_02 - method setup -
-- test02 --
PASSED- method teardown -
- class teardown -
(2)fixture
函数级别
tmp.py
import pytest
@pytest.fixture(scope='module')
def fix_module():
print('-- module setup --')
yield
print('-- module teardown --')
@pytest.fixture()def fix_func(fix_module):
print('-- func setup --')
yield
print('-- func teardown --')
@pytest.fixture()def fix_func2():
print('-- func2 setup --')
yield
print('-- func2 teardown --')
def test_01(fix_func):
print('-- test 01 --')
def test_02(fix_func2):
print('-- test 02 --')
scope="module", module 级别
yeild 关键字
class 类级别
import pytest
@pytest.fixture(scope='module')
def fix_module():
print('-- module setup --')
yield
print('-- module teardown --')
class TestTmp2:
@pytest.fixture()
def fix_func(self, fix_module):
print('-- func setup --')
yield
print('-- func teardown --')
def test_01(self,fix_func):
print('-- test 01 --')
def test_02(self,fix_func):
print('-- test 02 --')
pytes -s -v tmp2.py
tmp2.py::TestTmp2::test_01 -- module setup --
-- func setup --
-- test 01 --
PASSED-- func teardown --
tmp2.py::TestTmp2::test_02 -- func setup --
-- test 02 --
PASSED-- func teardown --
-- module teardown --
tmp2.py
import pytest
@pytest.fixture(scope='module')
def fix_module():
print('-- module setup --')
yield
print('-- module teardown --')
@pytest.fixture(scope='session')
def fix_session():
print('-- session set up --')
yield
print('-- session teardown --')
@pytest.fixture(scope='class')
def fix_class():
print('-- class set up --')
yield
print('-- class tear down --')
@pytest.fixture(scope='function')
def fix_function():
print('-- function set up --')
yield
print('-- function tear down --')
@pytest.mark.usefixtures('fix_session','fix_module','fix_class' ,'fix_function')
class TestTmp2:
def test_01(self):
print('-- test 01 --')
def test_02(self):
print('-- test 02 --')
● session: 所有
● module: 整个文件
● class:类
● function:方法
testcases/testfixture/tmp2.py::TestTmp2::test_01
-- session set up --
-- module setup --
-- class set up --
-- function set up --
-- test 01 --
PASSED-- function tear down --
testcases/testfixture/tmp2.py::TestTmp2::test_02
-- function set up --
-- test 02 --
PASSED-- function tear down --
-- class tear down --
-- module teardown --
-- session teardown --
conftest.py 多个文件共享
4、参数化
(1)mark.parametrize
import pytest
a = [
('share','title1','conent1'),
('share','title2','conent2'),
]
@pytest.mark.parametrize('tab,title,content',a)
def test_a(tab,title,content):
print('----',tab,title,content,'-----')
(2)fixture 级别的参数化
import pytest
@pytest.fixture(params=[0,1],ids=['spam','ham'])
def a(request):
return request.param
def test_a(a):
print(f'--{a}--')
# assert a
def test_b(a):
print(f'=={a}==')
以上就是Pytest快速入门使用说明手册的全部内容,大家如果觉得本文对你有帮助,不妨把文章分享出去,让更多的人看到~
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
参加自动化性能测试培训需要关注哪些问题?
在互联网+与双创政策的刺激下,国内互联网领域一直处于井喷式的发展模式。优秀的产品层叠出现,越来越多的公司开始注重产品上线前后的性能测试工作。通过性能测试,公司可以在投入运行之前检验应用是否满足业务需求,量化终用户体验的变化的影响,并且快速分析和解决发生故障的组件,因而在部署时就较少出现意外。从而使性能测试成为炙手可热的岗位之一。那我们参加自动化性能测试培训需要关注哪些问题呢?
6353
2019-07-24 18:35:57
刚入行软件测试,去大公司好还是小公司好?
软件测试是目前国内发展比较快速的互联网岗位之一,很多人都看准机会加入到这个行业中,面对择业的时候,和大部分职场小白或者优秀毕业生一样,都会比较纠结到底选择什么样的公司就职?下面小编就和大家一起分析一下刚入行软件测试,去大公司好还是去小公司好?
7630
2019-08-07 18:11:23
什么是软件测试?软件测试做什么?
什么是软件测试?软件测试做什么?软件测试是在测试中识别软件产品和服务的准确性和质量的过程。软件测试的诞生是为了验证产品是否满足客户的特定先决条件、需求和需求。在这个过程中,您将回顾产品、系统,并考虑用户真正想要什么和已经做了什么,在软件测试中,错误、缺陷和错误之间有区别,我们应该清楚地区分,以避免误解问题。
5730
2019-08-12 11:57:07
去培训机构学软件测试怎么样?
对于初次接触软件测试的人来说,测试培训能够引领学习者更快速的入门,从学习开始能够从正确的方向理解软件测试,对后期进一步学习是特别有帮助,在后面的学习中能够起到事半功倍的效果。
1609
2021-01-20 16:26:10
软件测试方法和技术知识点有哪些?
软件测试是软件开发过程的重要组成部分,用来确认一个程序的品质或性能是否符合开发之前所提出的一些要求。软件测试人员要寻找Bug,避免软件开发过程中的缺陷,衡量软件的品质,关注用户的需求,总的目标确保软件的质量。
1228
2021-02-26 10:44:51
热门文章
- JavaEE极速就业班课程怎么样?有哪些优势?
- 狂野大数据课程怎么样?项目实战多吗?
- 狂野架构师课程培训哪些内容?能学会吗?
- Spark SQL 结构化数据处理流程及原理是什么?
- Java互联网架构师就业前景怎么样?待遇好不好?
- 狂野架构师课程怎么样?Java架构师学什么内容?
- 狂野大数据厉害吗?可以提升哪些核心技能?
- 缓存淘汰策略有几种?要怎么用?
- 软件测试极速就业班课程怎么样?零基础能学吗
- 狂野架构师学习效果好不好?互联网Java架构师前景怎么样? 查看更多
扫描二维码,了解更多信息
