优就业是中公教育IT培训品牌,致力于培养面向互联网领域的人才,以学员就业为目的,就业为宗旨,是一家集互联网营销师、UI/UE交互设计师、Web前端工程师、Java工程师、Python工程师、Unity开发工程师、大数据工程师、Linux云计算工程师、PHP工程师等课程为一体的IT培训机构。为培养符合时代需求的IT人才,中公教育优就业以高瞻的视野,经多年布局,打造人才培训服务体系。以企业需求为导向,以行业未来为驱动,向企业和社会不断输送IT人才。
优就业课程优势培育软件测试工程师
课程研发深思熟虑、不断更新迭代
测试工具
学习LoadRunner、JMeter、Selenium、Appium、Postman、Fiddler等测试工具。
前沿开发语言
学习前沿开发语言Python,打下自动化测试的基础。
测试技术
课程内容涵盖功能、web自动化、移动自动化、接口、性能主流测试技术。
测试领域
按照企业工作流程实训演练,毕业后可进入金融、大数据、电商、移动APP、游戏、企业级应用软件等领域
课程设置
第 一阶段-测试基础
第二阶段-Python编程
第三阶段-Web端测试
第四阶段-移动端测试
第五阶段-服务端测试
第六阶段-性能测试
第七阶段-就业指导
Pytest作为一个Python测试框架,不仅简单灵活,新手也可以入门,而且具有很多的第三方插件,功能十分强大。因此,Pytest可以说是测试行业从业者必学的工具。本文将为大家介绍Pytest的安装、基本操作、运行时设置,以及参数化,下面一起来拿看看Pytest入门使用说明手册吧!
1、安装
(1)全局安装
使用pip进行安装
pipinstall-Upytest
检查安装版本
$pipinstall-Upytest
Thisispytestversion4.4.0,importedfromxxxx
(2)项目目录下安装
如果只是将pytest安装到当前项目中,不与其它的版本混淆,使用virtualenv进行安装
mkdirpytest_project
cdpytest_project
virtualenv.venv
这将会在项目目录下创建pytest-env目录存放虚拟环境。
激活虚拟环境
source.venv/bin/activate
再次使用pip进行安装,安装文件将存放在当前项目目录下,而不再是全局环境变量下
$pipinstallpytest
(3)区别
全局安装方式适合所有项目,在项目目录下虚拟化安装只适合当前项目。
2、基本操作
(1)主要内容
基本使用方式:我们将从一个简单的测试开始,Pytest命名规范文件名以test_开头或以_test.py结尾。首先创建一个名为test_capitalize.py的文件,在此文件中编写一个名为capital_case的函数,以字符串作为参数,将参数转化为大写返回。另外编写一个test_capital_case参数函数,主要验证capital_case函数完成它所说的内容,我们用test_作为测试函数名称的前缀。
#test_capitalize.py
defcapital_case(x):
returnx.capitalize()
deftest_capital_case():
assertcapital_case('semaphore')=='Semaphore'
在命令行运行pytest, 将自动执行test_capitalize.py文件中的test_capital_case测试方法;
collected1item
test_capitalize.py. []
=========================================1passedin0.01seconds==========================================
测试用例执行通过。
3、Pytest运行时设置
(1)xunit格式
函数级别设置运行时
setup_module
setup
teardown
teardown_module
如下代码
defsetup_module():
print("module--setup--")
defteardown_module():
print('module--teardown--')
defsetup():
print("function--setup--")
defteardown():
print("function--teardown--")
deftest_01():
print("---test01---")deftest_02(): print('-----test02------')
运行文件pytest-s-vtmp.py
testcases/tmp.py::test_01
module--setup--
function--setup
-----test01---
PASSEDfunction--teardown--
testcases/tmp.py::test_02
function--setup--
-----test02------
PASSED
function--teardown--
module--teardown--
Class类级别
tmp2.py
classTestTmp2:
@classmethod
defsetup_class(cls):
print('-classsetup-')
@classmethod
defteardown_class(cls):
print('-classteardown-')
defsetup(self):
print('-methodsetup-')
defteardown(self):
print("-methodteardown-")
deftest_01(self):
print("--test01--")
deftest_02(self):
print('--test02--')
pytest-s-vtmp2.py
tmp2.py::TestTmp2::test_01-classsetup
--methodsetup-
--test01--
PASSED-methodteardown-
testcases/tmp/tmp2.py::TestTmp2::test_02-methodsetup-
--test02--
PASSED-methodteardown-
-classteardown-
(2)fixture
函数级别
tmp.py
importpytest
@pytest.fixture(scope='module')
deffix_module():
print('--modulesetup--')
yield
print('--moduleteardown--')
@pytest.fixture()deffix_func(fix_module):
print('--funcsetup--')
yield
print('--functeardown--')
@pytest.fixture()deffix_func2():
print('--func2setup--')
yield
print('--func2teardown--')
deftest_01(fix_func):
print('--test01--')
deftest_02(fix_func2):
print('--test02--')
scope="module",module级别
yeild关键字
class类级别
importpytest
@pytest.fixture(scope='module')
deffix_module():
print('--modulesetup--')
yield
print('--moduleteardown--')
classTestTmp2:
@pytest.fixture()
deffix_func(self,fix_module):
print('--funcsetup--')
yield
print('--functeardown--')
deftest_01(self,fix_func):
print('--test01--')
deftest_02(self,fix_func):
print('--test02--')
pytes-s-vtmp2.py
tmp2.py::TestTmp2::test_01--modulesetup--
--funcsetup--
--test01--
PASSED--functeardown--
tmp2.py::TestTmp2::test_02--funcsetup--
--test02--
PASSED--functeardown--
--moduleteardown--
tmp2.py
importpytest
@pytest.fixture(scope='module')
deffix_module():
print('--modulesetup--')
yield
print('--moduleteardown--')
@pytest.fixture(scope='session')
deffix_session():
print('--sessionsetup--')
yield
print('-- sessionteardown--')
@pytest.fixture(scope='class')
deffix_class():
print('--classsetup--')
yield
print('--classteardown--')
@pytest.fixture(scope='function')
deffix_function():
print('--functionsetup--')
yield
print('--functionteardown--')
@pytest.mark.usefixtures('fix_session','fix_module','fix_class','fix_function')
classTestTmp2:
deftest_01(self):
print('--test01--')
deftest_02(self):
print('--test02--')
●session:所有
● module:整个文件
●class:类
●function:方法
testcases/testfixture/tmp2.py::TestTmp2::test_01
--sessionsetup--
--modulesetup--
--classsetup--
--functionsetup--
--test01--
PASSED--functionteardown--
testcases/testfixture/tmp2.py::TestTmp2::test_02
--functionsetup--
--test02--
PASSED--functionteardown--
--classteardown--
--moduleteardown--
-- sessionteardown--
conftest.py多个文件共享
4、参数化
(1)mark.parametrize
importpytest
a=[
('share','title1','conent1'),
('share','title2','conent2'),
]
@pytest.mark.parametrize('tab,title,content',a)
deftest_a(tab,title,content):
print('----',tab,title,content,'-----')
(2)fixture级别的参数化
importpytest
@pytest.fixture(params=[0,1],ids=['spam','ham'])
defa(request):
returnrequest.param
deftest_a(a):
print(f'--{a}--')
#asserta
deftest_b(a):
print(f'=={a}==')
以上就是Pytest入门使用说明手册的全部内容,大家如果觉得本文对你有帮助,不妨把文章分享出去,让更多的人看到~