pytest

Pytest fixtures使用详解

老子叫甜甜 提交于 2021-02-16 13:57:35
当我们想在每个测试方法之前运行一些代码时,将使用夹具。 因此,我们定义夹具而不是在每个测试中都重复相同的代码。 通常,固定装置用于初始化数据库连接,传递基数等 通过将标记为 @ pytest.fixture 通过提及固定装置作为输入参数,测试方法可以使用固定装置。 使用以下代码创建一个新文件test_basic_fixture.py import pytest @pytest.fixture def supply_AA_BB_CC () : aa= 25 bb = 35 cc= 45 return [aa,bb,cc] def test_comparewithAA (supply_AA_BB_CC) : zz= 35 assert supply_AA_BB_CC[ 0 ]==zz, "aa and zz comparison failed" def test_comparewithBB (supply_AA_BB_CC) : zz= 35 assert supply_AA_BB_CC[ 1 ]==zz, "bb and zz comparison failed" def test_comparewithCC (supply_AA_BB_CC) : zz= 35 assert supply_AA_BB_CC[ 2 ]==zz, "cc and zz comparison failed

pytest学习笔记

若如初见. 提交于 2021-02-16 13:22:59
pytest 优于其他测试框架的地方: 1、简单的测试可以简单的写 2、复杂的测试也可以简单的写 3、测试的可读性强 4、易于上手 5、断言失败仅使用原生assert关键字,而不是self.assertEqual()或者self.assertLessThan() 6、pytest可以运行有unitest和nose编写的测试用例 pytest不依赖python的版本,python2和3都能安装最新版的pytest Tasks项目: tasks程序通过CLI交互,底层编码通过调用API实现 很久没有更新博客园,比较好听点的原因是由于项目比较忙,没有时间整理写脚本过程中的问题。实际上是由本人的“懒”,不想写。不写不记录慢慢的就导致了懒癌的形成,久而久之某些知识回了之后就忘记了,好了 废话不多说 记录下最近写代码过程中的一些问题 1、写接口脚本时候,为了实现活动榜单数据制造,需要写一个登录方法 从json返回值中取出token、uid(uid 用来造主播榜单 给不同的主播送礼,token 不同用户给同一个主播送礼,造用户排行榜的榜单)。礼用config文件存储,将多个token从接口中读出存放在list,然后写入config.txt文件中,用来后面送礼 接口读取数据。但是发下config只能存入字符串str,于是后面想一个个的读取就失败了。 如果使用如下方法直接转换成list 不能实现

学习-Pytest(二)执行用例规则

会有一股神秘感。 提交于 2021-02-16 12:31:45
1. 执行方式 cmd执行pytest用例有三种方法,以下三种方法都可以,一般推荐第一个 pytest py.test python -m pytest 如果不带参数,在某个文件夹下执行时,它会查找该文件夹下所有的符合条件的用例(查看用例设计原则) 2. 执行规则 1.执行某个目录下所有的用例 pytest 文件名/ 2.执行某一个py文件下用例 pytest 脚本名称.py 3.-k 按关键字匹配 pytest -k “MyClass and not method” 4.按节点运行 运行.py模块里面的某个函数 pytest test_mod.py::test_func 运行.py模块里面,测试类里面的某个方法 pytest test_mod.py::TestClass::test_method 5.标记表达式 pytest -m slow 将运用@pytest.mark.slow装饰器修饰的所有测试 6.从包里面运行 pytest —pyargs pkg.testing 这将导入 pkg.testing 并使用其文件系统位置来查找和运行测试。 7.pytest -x( 遇到错误时停止测试) pytest -x test_class.py 8.pytest -maxfail=num(当用例错误个数达到指定数量时,停止测试) pytest —maxfail=1 3.

pytest文档15-使用自定义标记mark

折月煮酒 提交于 2021-02-14 12:16:43
前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行。一个大项目自动化用例时,可以划分多个模块, 也可以使用标记功能,标明哪些是模块1用例,哪些是模块2的,运行代码时候指定mark名称运行就可以 mark标记 1.以下用例,标记test_send_http()为webtest # content of test_server.py import pytest @pytest.mark.webtest def test_send_http(): pass # perform some webtest test for your app def test_something_quick(): pass def test_another(): pass class TestClass: def test_method(self): pass if __name__ == "__main__": pytest.main(["-s", "test_server.py", "-m=webtest"]) 只运行用webtest标记的测试,cmd运行的时候,加个-m 参数,指定参数值webtest $ pytest -v -m webtest ============================= test session starts ===

Is there a custom way to collect pytest results?

蓝咒 提交于 2021-02-11 15:41:07
问题 I want to see the results of the tests that just run such that I can pipe the information e.g. into a custom file. This will be used later on to automatically parse the pytest results. 回答1: Yes there is a way by creating / using the conftest.py. In the example a dict with the results will be created and after all tests have run the results will printed. from collections import OrderedDict test_results = OrderedDict() def get_current_test(): """Just a helper function to extract the current

Is it possible to skip a test generated from multiple @pytest.mark.parametrize lines?

孤街醉人 提交于 2021-02-11 14:41:42
问题 I have a pytest test function with multiple @pytest.mark.parametrize lines. The values for each parameterised variable can take either 0 or 1. Some of the combination generated cannot happen and should be skipped i.e. in my example when subcontact takes the value 0 and when fast track takes the value 1, it should be skipped. Is it possible to achieve this with the multiple @pytest.mark.parametrize fixture? I attempted this with the code below but no tests were reported as skipped when I ran

Python unittest patch mock entier class

依然范特西╮ 提交于 2021-02-11 13:14:33
问题 I have a class that I want to patch in my unittests. class OriginalClass(): def method_a(): # do something def method_b(): # do another thing Now I created another class to patch it with, so the code for patching it is like class MockClass(OriginalClass): def method_a(): # This will override the original method and return custom response for testing. patcher = patch('OriginalClass', new=MockClass) mock_instance = patcher.start() This works exactly as I want it to and I can return whatever

Python unittest patch mock entier class

邮差的信 提交于 2021-02-11 13:11:42
问题 I have a class that I want to patch in my unittests. class OriginalClass(): def method_a(): # do something def method_b(): # do another thing Now I created another class to patch it with, so the code for patching it is like class MockClass(OriginalClass): def method_a(): # This will override the original method and return custom response for testing. patcher = patch('OriginalClass', new=MockClass) mock_instance = patcher.start() This works exactly as I want it to and I can return whatever

In pytest print text to testrail comment

Deadly 提交于 2021-02-11 12:55:50
问题 I am using pytest-testrail plugin to upload test result to testrail. I'd like to post the output from my test program (usually using a print command) to testrail also. How can we do this? I tried something like pytest -rA or -s etc. without success. for example, one of my test cases: @pytestrail.case('C18346') @pytest.mark.hw @pytest.mark.debug def test_cputemp(): cmd = "cat /sys/class/hwmon/hwmon1/temp*_input" r = ssh_client.execute(cmd) print() print(r['out']) print("Please check the