pytest

Python Pytest unpack fixture

老子叫甜甜 提交于 2019-12-31 03:56:05
问题 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.

pytest 参数化中的indirect的作用

戏子无情 提交于 2019-12-30 22:40:47
总结:当indirect为True的时候,参数为固件函数名称的,执行的时候会当做函数来执行。 当indirect为false的时候,参数为固件函数名称的,执行的时候会当做一个参数来执行。 参考下面的例子 # coding:utf-8 """ author:zhouxuan 2019/12/27 17:01 PyCharm """ import pytest @pytest.fixture(params=[1,2,3]) def fixture_param(request): request.param print("\033[31;1m我是fixture_param,这是第%s次打印\033[0m"%request.param) return request.param @pytest.mark.parametrize("fixture_param",["a","b",'c'],indirect=True) @pytest.mark.parametrize("a,b",[(1,6),(2,3)]) def test_fixture_param_and_parametrize(a,b,fixture_param): print("我是测试函数test_fixture_param_and_parametrize,参数a是%s,b是%s"%(a,b)) # print("我fixture

How to test the pytest fixture itself?

雨燕双飞 提交于 2019-12-30 08:40:31
问题 What is the proper way to test that the pytest fixture itself. Please do not confuse it with using fixture in tests. I want just tests the fixtures correctness by itself. When trying to call and execute them inside test I am facing: Fixture "app" called directly. Fixtures are not meant to be called directly Any input on that will be appreciated. Docs on this topic are not giving me meaningful guidance: https://docs.pytest.org/en/latest/deprecations.html#calling-fixtures-directly The

How to make py.test run doctests as well as normal tests directory?

我们两清 提交于 2019-12-30 03:47:09
问题 We currently have py.test with the coverage plugin running over our tests in a tests directory. What's the simplest way to also run doctests extracted from our main code? --doctest-modules doesn't work (probably since it just runs doctests from tests ). Note that we want to include doctests in the same process (and not simply run a separate invocation of py.test ) because we want to account for doctest in code coverage. 回答1: Now it is implemented :-). To use, either run py.test --doctest

Use docstrings to list tests in py.test

和自甴很熟 提交于 2019-12-30 02:43:08
问题 Here is a simple test file: # test_single.py def test_addition(): "Two plus two is still four" assert 2 + 2 == 4 def test_addition2(): "One plus one is still two" assert 1 + 1 == 2 The default output in py.test is like $ py.test test_single.py -v [...] test_single.py::test_addition PASSED test_single.py::test_addition2 PASSED I would like to have Two plus two is still four PASSED One plus one is still two PASSED i.e. use the docstrings as descriptions for the tests. I tried to use a

pytest fixture of fixtures

不想你离开。 提交于 2019-12-29 06:23:09
问题 I am currently writing tests for a medium sized library (~300 files). Many classes in this library share the same testing scheme which were coded using pytest: File test_for_class_a.py: import pytest @pytest.fixture() def setup_resource_1(): ... @pytest.fixture() def setup_resource_2(): ... @pytest.fixture() def setup_class_a(setup_resource_1, setup_resource_2): ... def test_1_for_class_a(setup_class_a): ... def test_2_for_class_a(setup_class_a): ... similar files exist for class_b, class_c

Pytest fixture for a class through self not as method argument

折月煮酒 提交于 2019-12-29 05:27:26
问题 Often I'll write a test class that uses a pytest fixture in every method. Here's an example. I'd like to be able to avoid having to write the fixture name in the signature of every method. It's not DRY. How can this be done? I would like to be able to access the fixture by giving the fixture as an attribute of the test class. In this example, I would like to see the google fixture as an attribute of TestGoogle. Is this possible? from bs4 import BeautifulSoup import pytest import requests

Using pytest with a src layer

十年热恋 提交于 2019-12-28 06:00:34
问题 pytest recommends including an additional directory to separate the source code within a project: my_package ├── src # <-- no __init__.py on this layer │ └── my_package │ ├── __init__.py │ └── util_module │ ├── __init__.py │ └── utils.py └── tests ├── __init__.py └── test_util_module ├── __init__.py └── test_utils.py Sadly, they say nothing 1 about how imports in the test code should work in such a case, which work for my IDE just fine in this naive example 2 , but causes the following error

pytest之解析ini文件

只愿长相守 提交于 2019-12-28 03:46:37
ini文件,可以用于存储自动化测试中,来管理测试的相关数据,存储格式简单,也便于维护,先简单了解下对于ini文件的基本操作 ex.ini文件 [test_section] test_param = test_value 姓名 = 张三 [test_section1] username = sam password = 123 [test_section2] from configobj import ConfigObj config = ConfigObj('ex.ini',encoding='UTF8') #读取 print (config['test_section1']) #直接读取 config=(dict(config)) #转为dict格式读取 for key,value in config.items(): print (key,'-----',value) config = ConfigObj('ex.ini',encoding='UTF8') config['test_section1']['username']='sam' config.write() #编辑,写入的时候不能从dict格式写 config['test_section2']={'username':'timop'} #写入 config.write() del config['test

PytestUnknownMarkWarning

只谈情不闲聊 提交于 2019-12-27 16:42:39
pytest框架通过打标签执行测试用例报PytestUnknownMarkWarning: Unknown pytest.mark.login_success - is this a typo?解决方案[TOC] 添加pytest.int 配置文件 这个方法单个标签和多个标签都适用 [pytest] markers = testmark1 testmark2 testmark3 在你项目的任意一个文件下,新建一个file,文件命名为pytest.int,如图运行后警告就没有了 来源: CSDN 作者: 阿望在呢 链接: https://blog.csdn.net/weixin_41955664/article/details/103730580