pytest

Pytest权威教程25-配置

陌路散爱 提交于 2019-12-02 09:42:45
目录 配置 命令行选项和配置文件设置 初始化:确定ROOTDIR和INIFILE 寻找rootdir 如何更改命令行选项默认值 内置的配置文件选项 返回: Pytest权威教程 配置 命令行选项和配置文件设置 你可以使用常规帮助选项获取有关INI选项配置文件中命令行选项和值的帮助: pytest -h # prints options _and_ config file settings 这将显示已安装插件注册的命令行和配置文件设置。 初始化:确定ROOTDIR和INIFILE pytest根据rootdir命令行参数(指定的测试文件,路径)以及ini-files的存在为每次测试运行确定一个。在启动过程中,确定的文件rootdir和ini文件将作为pytest标头的一部分打印。 以下是摘要的pytest用途rootdir: 在收集期间构造nodeid;每个测试都分配有一个唯一的nodeid,该id植根于,rootdir并考虑了完整路径,类名,函数名和参数化(如果有)。 插件将其用作存储项目/测试运行特定信息的稳定位置;例如,内部缓存插件在其中创建一个.pytest_cache子目录rootdir来存储其交叉测试运行状态。 需要特别强调的是,rootdir它不用于修改sys.path/PYTHONPATH或影响模块的导入方式。有关更多详细信息,请参见:Pytest导入机制和系统路径。

1、pytest -- 安装和入门

梦想与她 提交于 2019-12-02 08:01:36
文章目录 1. 安装 2. 创建你的第一个测试用例 3. 执行多个测试用例 4. 触发一个指定异常的断言 5. 在一个类中组织多个测试用例 6. 申请一个唯一的临时目录 pytest 是一个能够简化测试系统构建、方便测试规模扩展的框架,它让测试变得更具表现力和可读性–模版代码不再是必需的。 只需要几分钟的时间,就可以对你的应用开始一个简单的单元测试或者复杂的功能测试。 1. 安装 命令行执行如下命令: pipenv install pytest==5.1.3 查看安装的版本信息: pipenv run pytest --version 2. 创建你的第一个测试用例 它只有四行代码: # src/chapter-1/test_sample.py def func ( x ) : return x + 1 def test_sample ( ) : assert func ( 3 ) == 5 通过以下命令执行测试: λ pipenv run pytest src/chapter-1/test_sample.py == == == == == == == == == == == == == == = test session starts == == == == == == == == == == == == == == == platform win32 -- Python 3.7

Accessing test file name from conftest.py

做~自己de王妃 提交于 2019-12-02 06:52:15
问题 What I'm trying to do I'm writing a small framework in python using pytest, and as part of the teardown I am taking a screenshot. Now, I want that screenshot to be named according to the running test, not conftest.py So, for example, my code right now is: driver.save_screenshot(os.path.basename(__file__)+'.png') The problem This prints the name "conftest.py". I want to print the calling test name if possible. I am guessing using requests? 回答1: You can access the current file path via request

Accessing test file name from conftest.py

大兔子大兔子 提交于 2019-12-02 06:40:22
What I'm trying to do I'm writing a small framework in python using pytest, and as part of the teardown I am taking a screenshot. Now, I want that screenshot to be named according to the running test, not conftest.py So, for example, my code right now is: driver.save_screenshot(os.path.basename(__file__)+'.png') The problem This prints the name "conftest.py". I want to print the calling test name if possible. I am guessing using requests? You can access the current file path via request.node.fspath . Example: # conftest.py import pytest @pytest.fixture def currpath(request): return str(request

pytest之mark功能

时间秒杀一切 提交于 2019-12-02 06:24:38
pytest系列(一)中给大家介绍了pytest的特性,以及它的编写用例的简单至极。 那么在实际工作当中呢,我们要写的自动化用例会比较多,不会都放在一个py文件里。 如下图所示,我们编写的用例存放在不同的py文件当中。 当我们想只运行诸多py文当中的部分用例,怎么办呢? 比如自动化工作当中,选择test_a,test_33,test_000这3个用例来运行的话,如何过滤呢? pytest.mark一下 在pytest当中,先给用例打标记,在运行时,通过标记名来过滤测试用例。 步骤1:给用例打标签 给用例打标记分为2个步骤: 1)注册标签名 官方提供的注册方式有2种,这里只提供一种最简单直接的方式: 通过pytest.ini配置文件注册。在pytest.ini文件当中: [pytest] # 固定的section名 markers= # 固定的option名称   标签名1: 标签名的说明内容。   标签名2   标签名N 示例如下: 2)在测试用例/测试类中给用例打标记(只能使用已注册的标记名) 在 测试用例的前面加上:@pytest.mark.已注册标签名 如下图,对3个测试文件当中的,要筛选出来的用例,都打了me标签 。 步骤2:运行时,根据用例标签过滤(-m 标签名) pytest提供了命令行参数来配置运行时的条件。 在命令行当中,输入pytest -

Limit the number of test cases to be executed in pytest

落爺英雄遲暮 提交于 2019-12-02 05:34:37
问题 A little background I am executing my test cases with Jenkins, I am doing a little POC with Jenkins right now. And, in my case, there are 500+ test cases which takes an hour to execute. I want only one test case to be executed just to know I didn't make any mistakes while doing my Jenkins POC. Is there a way to limit number of test case to be executed ? something like.. pytest -vv --limit 1 or using conftest.py? 回答1: You can limit the amount of tests in many ways. For example, you can execute

pytest小结

一世执手 提交于 2019-12-02 05:26:59
一.pytest简洁和好处 自动发现 testloader 断言方便 assert 1==1 灵活运行指定的测试用例,标签化,回归 正向 冒烟 登陆 环境管理灵活。会话 模块 那个用哪个不用 fixture:setUp setIPClass setUpModel 丰富的插件,测试报告,插件介绍 allure(比较成熟的测试报告体系,unittest不支持这个插件) 和unittest / nose兼容 二 pytest运行方式 当前文件夹,裕兴的哪个目录,看下没有的情况运行 自动发现测试用例: 1.文件名test_*.py 和 *_test.py开头或结尾,有类名,必须以Test开头的类,没有__init__函数,测试方法名以test_开头的函数 2.没有类,那么以函数为单位的函数名必须以test_开头 三/断言: 自定义提示文案 assert 1==1, "提示文案" 三。 pytest 命令行 python -m pytest ,unittest 编辑器:Run -->run-->Edit Configurations-->+ -->python test -->pytest -->Run 尽量以项目名打开 python代码:添加test_cases文件夹, main.py import pytest pytest.main()/i 自定义查找规则(在根目录下pytest

Python Pytest unpack fixture

断了今生、忘了曾经 提交于 2019-12-02 05:23:48
I have a fixture that creates a list of items during tests. I want to have another fixture which is parametrized with values generated by the first one. Example code import random import pytest @pytest.fixture def values(): return [random.randint(0, 100) for _ in range(10)] @pytest.fixture def value(request): return request.param @pytest.mark.parametrize("value", params=values): def test_function(value): assert value > 0 The problem with above code is that values is a function and not a list. I did quite a lot of digging but didnt find any way to unpack fixture to parametrize another with it.

pytest running scenarios in the correct order in the class

陌路散爱 提交于 2019-12-02 04:35:17
So I have the following structure: class Test(object): def test_1(self): pass def test_2(self): pass def test_3(self): pass it runs great, NOW I'm adding the "scenarios" (as it's recommended at pytest - A quick port of “testscenarios” ): def pytest_generate_tests(metafunc): idlist = [] argvalues = [] for scenario in metafunc.cls.scenarios: idlist.append(scenario[0]) items = scenario[1].items() argnames = [x[0] for x in items] argvalues.append(([x[1] for x in items])) metafunc.parametrize(argnames, argvalues, ids=idlist) class Test(object): scenarios = ['1' {'arg':'value1'}, '2' {'arg':'value2'

Running pytest with cython - how to compile cython modules in pytest?

心已入冬 提交于 2019-12-02 04:08:46
I have a project organized as follows: project ├── project │ ├── module1 │ │ ├── api.py │ │ ├── _cpython_foo.py │ │ └── _cython_foo.pyx │ └── module2 ├── setup.py └── tests └── module1 ├── test_cython_foo.py └── test_cpython_foo.py where api.py imports cythonized extensions: """api.py"""" from _cython_foo import cython_fun My setup script builds the .pyx source inplace correctly and I'm able to use cython_fun in the installed package: import project.module1.api as module1 module1.cython_fun() # OK However, pytest complains that it cannot import the cython module since the compiled binaries are