pytest

Python:Python 自动化测试框架 unittest 和 pytest 对比

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 04:02:59
一、用例编写规则 1.unittest提供了test cases、test suites、test fixtures、test runner相关的类,让测试更加明确、方便、可控。使用unittest编写用例,必须遵守以下规则:   (1)测试文件必须先import unittest   (2)测试类必须继承unittest.TestCase   (3)测试方法必须以“test_”开头   (4)测试类必须要有unittest.main()方法  2.pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效。使用pytest编写用例,必须遵守以下规则: (1)测试文件名必须以“test_”开头或者"_test"结尾(如:test_ab.py)   (2)测试方法必须以“test_”开头。   (3)测试类命名以"Test"开头。  总结: unittest :用例格式--复杂,不能兼容pytest用例;     pytest: 用例格式--简单,可以兼容unittest用例; 二、用例前置和后置 1.unittest提供了setUp/tearDown,只能针对所有用例。 2.pytest提供了模块级、函数级、类级、方法级的setup/teardown,比unittest的setUp/tearDown更灵活。 模块级(setup

selenium-项目实战-4

怎甘沉沦 提交于 2019-11-30 03:04:38
1:pytest的conftest文件的编写 from selenium import webdriverfrom config.url_config.choose_test_domain_name import choose_domain_nameimport pytestfrom common.logger import Loggerfrom test_page.login_page import LoginPagefrom test_page.add_address_page import AddAddressPagefrom test_page.address_manage_page import AddressManagePagelogger=Logger().get_logger()@pytest.fixture()def login_driver(): driver=webdriver.Chrome() login_url=choose_domain_name()+"doctor/login.html" logger.info("测试请求的网址是{}".format(login_url)) driver.get(login_url) driver.maximize_window() yield driver driver.quit() logger.info(

How to concatenate several parametrized fixtures into a new fixture in py.test?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 02:51:16
问题 If I have two parametrized fixtures, how can I create a single test function that is called first with the instances of one fixture and then with the instances of the other fixture? I guess it would make sense to create a new fixture that somehow concatenates the two existing fixtures. This works well for "normal" fixtures, but I don't seem to get it to work with parametrized fixtures. Here is a simplified example of what I tried: import pytest @pytest.fixture(params=[1, 2, 3]) def lower

Can I perform multiple assertions in pytest?

时间秒杀一切 提交于 2019-11-30 02:22:17
I'm using pytest for my selenium tests and wanted to know if it's possible to have multiple assertions in a single test? I call a function that compares multiple values and I want the test to report on all the values that don't match up. The problem I'm having is that using "assert" or "pytest.fail" stops the test as soon as it finds a value that doesn't match up. Is there a way to make the test carry on running and report on all values that don't match? As Jon Clements commented, you can fill a list of error messages and then assert the list is empty, displaying each message when the

Python与C/C++相互调用(转)

微笑、不失礼 提交于 2019-11-30 02:18:21
原文链接 作者 一、问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结。 二、Python调用C/C++ 1、Python调用C动态链接库 Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。 (1)C语言文件:pycall.c /***gcc -o libpycall.so -shared -fPIC pycall.c*/ #include <stdio.h> #include <stdlib.h> int foo(int a, int b) { printf("you input %d and %d\n", a, b); return a+b; } (2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。 (3)Python调用动态库的文件:pycall.py import ctypes ll = ctypes.cdll.LoadLibrary lib = ll("./libpycall.so") lib.foo(1, 3) print '***finish***' (4)运行结果: 2、Python调用C++(类

pytest之参数化parametrize的使用

送分小仙女□ 提交于 2019-11-30 00:55:18
在测试用例的前面加上: @pytest.mark.parametrize("参数名",列表数据) 参数名:用来接收每一项数据,并作为测试用例的参数。 列表数据:一组测试数据。 示例代码: import pytest test_datas = [ (11, 22, 33), (22, 33, 55) ] datas_dict = [ {"a": 1, "b": 2, "c": 3}, {"a": 11, "b": 22, "c": 33}, {"a": 111, "b": 222, "c": 333}, ] # 方式一:直接写 @pytest.mark.parametrize("a, b, c", [(1, 2, 3), (4, 5, 9)]) def test_add01(a, b, c): res = a + b assert res == c # 方式二:参数为列表中嵌套元组 @pytest.mark.parametrize("data", test_datas) def test_add02(data): res = data[0] + data[1] assert res == data[2] # 方式三:参数为列表中嵌套字典 @pytest.mark.parametrize("data", datas_dict) def test_add03(data): res =

parameterized test with cartesian product of arguments in pytest

空扰寡人 提交于 2019-11-30 00:42:46
Just wondering, is there any (more) elegant way of parameterizing with the cartesian product? This is what I figured out so far: numbers = [1,2,3,4,5] vowels = ['a','e','i','o','u'] consonants = ['x','y','z'] cartesian = [elem for elem in itertools.product(*[numbers,vowels,consonants])] @pytest.fixture(params=cartesian) def someparams(request): return request.param def test_something(someparams): pass At least I'd like to encapsulate numbers, vowels, consonants and cartesian in the fixture function. I can think of two ways to do this. One uses parametrized fixtures, and one parametrizes the

pytest介绍、安装及如何自动识别测试用例

淺唱寂寞╮ 提交于 2019-11-30 00:40:14
pytest:基于unittest之上的单元测试框架,其特征有: 1、自动识别测试用例和测试方法(unittest当中,需要引入TestSuite,主动加载测试用例) 2、简单的断言表达:assert 表达式即可。(unittest当中,self.assert*) 3、可以设置会话级(从运行所有用例开始到用例结束)、模块(.py)级、类级(setUpClass/teardownClass)、函数级的fixtures(数据准备+清理工作)(unittest当中是测试类 4、测试用例不一定要放在测试类中(unittest中需要自定义类并继承TestCase) 5、有非常丰富的插件,比如allure插件。(unittest无) 安装命令: pip install pytest 安装html报告的插件: pip install pytest-html 收集测试用例规则: 1、默认从当前目录中搜集测试用例,即在哪个目录下运行pytest命令,则从哪个目录中搜索 2、搜索规则: 1)、符合命名规则test_*.py或者*_test.py 2)、以test_开头的函数名 3)、以Test开头的测试类(没有__init__函数)当中,以test_开头的函数 来源: https://www.cnblogs.com/benben-wu/p/11541831.html

Python project directory structure / pytest trouble

白昼怎懂夜的黑 提交于 2019-11-29 22:58:24
This should be the easiest problem on earth, but even after extensive searching and tinkering, I'm still in deep trouble with finding a "correct" way to lay a directory structure and manage to run pytest etc correctly. Let's say my I have a program called apple. |- README.md |- apple | |-- __init__.py | |-- apple.py | - tests | |-- test_everything.py The apple.py contains some functions, for examples sake let's call one eat() . And the test_everything.py file contains some tests like assert eat()=="foobar" . So good so easy, but then the fun begins: What about the __init__.py in the apple

pytest

可紊 提交于 2019-11-29 20:55:44
全功能Python测试框架:pytest   python通用测试框架大多数人用的是unittest+HTMLTestRunner,这段时间看到了pytest文档,发现这个框架和丰富的plugins很好用,所以来学习下pytest. image.png pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点: 简单灵活,容易上手 支持参数化 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests) pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等 测试用例的skip和xfail处理 可以很好的和jenkins集成 report框架----allure 也支持了pytest 安装pytest: pip install -U pytest 验证安装的版本: pytest --version 几个pytest documentation中的例子: 例子1: import pytest # content of test_sample.py def