########################pytest笔记############################ # 创建第一个pytest测试 # import pytest # def func(x): # return x + 1 # def test_answer(): # assert func(3) == 5 # # 欲生成 pytest报告需要通过此方式运行程序,也可通过cmd运行命令:pytest -v -s 文件名 # if __name__ == '__main__': # pytest.main(['interface_auto.py', '-v']) #这里穿插一下pytest用例的执行知识: # 如果想执行单独一个函数或者类里面的方法,则输入pytest 文件名::类名::方法 #如果运行很多个测试用例,如果遇到有一个错误则停止执行则输入:pytest -v -s -x 文件名 #如果运行发现超出两个测试用例失败则停止执行,则输入:pytest --maxfail=2 #等等 # # # 运行多条测试,pytest会执行当前目录及子目录下所有test_*.py及*_test.py格式的文件 # import pytest # def f(): # raise SystemExit(1) # def test_mytest(): # with pytest.raises(SystemExit): # f() # # 然后通过cmd输入命令行以达到“安静模式”来执行这个测试方法: pytest -q E:\python\Project\venv\interface_auto.py # # 输出结果: [100%] 1 passed in 0.02s # 功能测试中请求使用独立的临时目录 # # pytest提供了内置fixtures及方法参数来请求任意资源,比如一个独立的临时目录: # def test_needsfiles(tmpdir): # print (tmpdir) # assert 0 # # 结果会创建唯一值的临时目录:C:\Users\hepeng\AppData\Local\Temp\pytest-of-hepeng\pytest-0\test_needsfiles0 # 使用和调用方法 # 通过python调用会将当前目录也添加到sys.path中,除此之外,这几乎等同于命令行直接调用pytest [...] # 6中不同的退出code见下: # 退出code 0: 收集并成功通过所有测试用例 # 退出code 1: 收集并运行了测试,部分测试用例执行失败 # 退出code 2: 测试执行被用户中断 # 退出code 3: 执行测试中发生内部错误 # 退出code 4: pytest命令行使用错误 # 退出code 5: 没有收集到测试用例 # 运行及调用方法 # 指定及选择测试用例---Pytest支持多种从命令行运行和选择测试用例的方法 # 运行模块内所有用例 # pytest test_mod.py # 运行目录内所有用例 # pytest testing/ # 按关键字表达式运行测试 # pytest -k "MyClass and not method" # 如果想执行单独一个函数或者类里面的方法,则输入pytest 文件名::类名::方法 #如果运行很多个测试用例,如果遇到有一个错误则停止执行则输入:pytest -v -s -x 文件名 #如果运行发现超出两个测试用例失败则停止执行,则输入:pytest --maxfail=2 #失败重新运行(重试):pytest-rerunfailures # 如:失败后重试3次:pytest --reruns 3 -v -s 文件名 # 失败后间隔3秒后重试2次:pytest -v --reruns-delay=2 #如果失败,忽略失败继续执行下去:assume,区别与assert失败一条案例后,后面的案例都不再执行 # 除此之外,还有很多返回结果自动校验: # assertEqual(a,b) a == b # assertNotEqual(a,b) a!= b # assertTrue(x) bool (x) is True # assertFalse(x bool (x) is False # assertIs(a,b) a is b # assertisNot(a,b) a is not b # assertisNone(x) × is None # assertIsNotNone(x) x is not None # assertin(a,b) a in b # assertNotin(a,b) a not in b # 通过标记表达式运行测试 # @pytest -m [标记名] # 这将会执行所有带@pytest.mark.[标记名] 的标记的用例 # # 使用assert语句进行断言,用于某条案例出错则后面案例都不在执行,区别于assume # def f(): # return 3 # def test_function(): # assert f % 2 == 0, "值为奇数,应为偶数" # 异常断言 # # test_foocompare.py内容 # class Foo(object): # def __init__(self, val): # self.val = val # # def __eq__(self, other): # return self.val == other.val # # def test_compare(): # f1 = Foo(1) # f2 = Foo(2) # assert f1 == f2 # pytest框架结构 #模块级别的setup和teardown优先级最高 #@pytest.fixture # fixtures作为函数参数使用 # 可以通过使用@pytest.fixture注册成为一个fixture函数,来为测试方法提供一个fixture对象 # 如现在有两个案例,一个需要用到login函数,一个不需要,则login作为参数传给需要用到的案例 # import pytest # @pytest.fixture() # # # 这个时候为了让测试案例1顺利继承到login函数,则需要定义 @pytest.fixture() # # # 如果这里不定义@pytest.fixture(),那么test_case01会报错,提示找不到login方法 # # #fixture中还可以scope中定义很多个参数,针对类级别的,包级别的,session级别的,统统都写在一个叫conftest.py(类似定义全局变量的方法,然后再从其他函数继承这个公共函数)中,这个后面做详细介绍 # def login(): # print("这是登录方法") # # def test_case01(login): # print("这是测试案例1") # def test_case02(): # print("这是测试案例2") #那么此刻就先调用登录函数,后在调用测试案例1和测试案例2 # 结果显示: # TEST_interface.py::test_case01 这是登录方法 # 这是测试案例1 # PASSED # TEST_interface.py::test_case02 这是测试案例2 # PASSED # 下面针对“fixture中还可以scope中定义很多个参数,针对类级别的,包级别的,session级别的,统统都写在一个叫conftest.py”这个说明做详细介绍 # 先看: # import pytest # @pytest.fixture(scope='class') # def login(): # print("这是登录方法") # # def test_case01(login): # print("这是测试案例1") # def test_case02(): # print("这是测试案例2") # # class Test_demo(): # def test_case03(self,login): #把login方法作为参数传进来 # print("这里是测试案例3") # # if __name__ == '__main__': # pytest.main(['-v','-s','interface_auto.py']) # 那么如果把login作为一个公共的方法放在其他文件中(conftest.py)使包下所有的方法都可以调用login中呢?则创建一个conftest.py: # 把登录这块儿单独放在conftest.py里面: # import pytest # @pytest.fixture() # def login(): # print("这是登录公共方法") # # 那么TEST_auto_case.py里面写成: # def test_case01(login): # print("这是测试案例1") # def test_case02(): # print("这是测试案例2") # # class Test_demo(): # def test_case03(self,login): #把login方法作为参数传进来 # print("这里是测试案例3") #项目实战: # ./conftest.py文件内容 import pytest from selenium import webdriver @pytest.fixture(scope="class") # fixtures作为函数参数使用 # 可以通过使用@pytest.fixture注册成为一个fixture函数,来为测试方法提供一个fixture对象 # 如现在有两个案例,一个需要用到OpenBaiDu函数,一个不需要,则OpenBaiDu作为参数传给需要用到的案例 def Test_OpenBaiDu(): print('这里是Test_OpenBaiDu方法打开浏览器操作') url='http://www.baidu.com' yield url #将url值返回才可以被其他文件所调用 # ./test_auto_case.py文件内容 import pytest from selenium import webdriver import pytest_assume import requests,time @pytest.fixture(scope="function") #如果不写pytest.fixture(),则下面的test_case02会报错提示not found test_case01 def case01(): print('pytest的case01') def test_case02(case01): print("pytest的test_case02") re =requests.get('http://www.baidu.com') code=re.status_code assert code==200 # pytest.assume(code==502) class Test_case(): def test_case03(self,Test_OpenBaiDu): print("这里调用了confftest.py文件里的Test_OpenBaiDu的方法") time.sleep(5) driver = webdriver.Chrome() driver.get(url=Test_OpenBaiDu) #这里要注意,调用参数要指向源头 driver.find_element_by_id('kw').send_keys('python使用排行榜') driver.find_element_by_id('su').click() time.sleep(1) driver.maximize_window() if __name__ == '__main__': pytest.main(['-v','-s','test_auto_case.py'])
#pytest测试框架入门学习到项目实战--个人手工学习笔记过程
最新推荐文章于 2025-04-24 19:54:05 发布