pytest

using fixture return value as value in mark.parametrize()

扶醉桌前 提交于 2019-12-10 10:05:02
问题 my question is - is it possible to use return value from fixture as a value in parametrize? The problem is - I'd like to dynamically get possible values (for example, available systems on a virtual server) for parametrize. I can access these when a virtual server is created by one of the fixtures. Tests look like this (pseudo-code-ish): [conftest.py] @pytest_fixture(scope='session') def test_server(request): test_server = Server([default_params]) test_server.add() def fin(): test_server

Pytest - Fixture introspect on function level

我与影子孤独终老i 提交于 2019-12-10 09:39:01
问题 I've got a fixture that requires a variable from the test function. Using introspection and declaring the variable in the function namespace/context should work if introspection on function level works, as it does for module level, but each time I run the code I end up with None instead of the string "Fancy Table". In the fixture I set the scope to 'function' and then introspect via getattr and request.function: #conftest.py @pytest.fixture(scope='function') def table(request): from data

Importing modules from a sibling directory for use with py.test

只愿长相守 提交于 2019-12-10 09:23:11
问题 I am having problems importing anything into my testing files that I intend to run with py.test. I have a project structure as follows: /ProjectName | |-- /Title | |-- file1.py | |-- file2.py | |-- file3.py | |-- __init__.py | |-- /test | |-- test_file1.py I have not been able to get any import statements working with pytest inside the test_file1.py file, and so am currently just attempting to use a variable declared in file_1.py and print it out when test_file1.py is run. file1.py contains:

passing a py.test fixture between test files in a module

无人久伴 提交于 2019-12-10 09:14:04
问题 I have a common py.test fixture that I want to use generically in different test files within the same module. After reading the py.test documentation, the suggestion was to add the fixture to a conftest.py file which should make the fixture available to all files in the module. But for some reason, I can't seem to get this common fixture to work with my test class. #In conftest.py import pytest @pytest.fixture def mock_data(scope="module"): return ({'number_of_females_1': 14, 'number_of

Python: accept unicode strings as regular strings in doctests

依然范特西╮ 提交于 2019-12-10 04:34:02
问题 Writing doctests for a method that abbreviates a dictionary by searching for a passed key word in the keys of the original dictionary, and returning the new, abbreviated dictionary. My docstring looks as follows: def abbreviate_dict(key_word, original_dict): """ >>> orig_dict = {apple_stems: 2, apple_cores: 5, apple_seeds: 3} >>> abbreviate_dict('apple', orig_dict) {'cores': 5, 'seeds': 3, 'stems': 2} """ etc. return new_dict The function works, but when I run py.test's doctest, the function

pytest--fixture参数化的实现方式和执行顺序

喜你入骨 提交于 2019-12-10 04:02:28
之前看到fixture函数可以通过添加,params参数来实现参数化,后续看到了 悠悠 的博客 ,可以通过 @pytest.mark.parametrize 来实现,现在做一个总结 实现方式一 通过params函数实现fixture的参数化 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 def test_fixture_param(fixture_param): print("我是test_fixture_param函数") # print("我fixture_param现在是:%s"%fixture_param) if __name__ == '__main__': pytest.main(["-s",'test_fixture_params.py']) 结果 输出的结果是这样的 大家都知道,使用fixture函数有两种方法 第一种是:直接将fixture函数的名字当做参数传入 另一种是:使用装饰器@pytest.mark.usefixtures("fixture函数名")

How to print output when using pytest with xdist

冷暖自知 提交于 2019-12-10 01:59:18
问题 I'm using py.test to run tests. I'm using it with pytest-xdist to run the tests in parallel. I want to see the output of print statements in my tests. I have: Ubuntu 15.10, Python 2.7.10, pytest-2.9.1, pluggy-0.3.1. Here's my test file: def test_a(): print 'test_a' def test_b(): print 'test_b' When I run py.test , nothing is printed. That's expected: by default, py.test captures output. When I run py.test -s , it prints test_a and test_b , as it should. When I run py.test -s -n2 , again

Invalid syntax in more-itertools when running pytest

断了今生、忘了曾经 提交于 2019-12-10 01:58:48
问题 I have the following minimal setup.py : import setuptools setuptools.setup( setup_requires=['pytest-runner'], tests_require=['mock', 'pytest'], test_suite='tests', python_requires='>=2.7', ) when running it with python setup.py test I keep getting the following error: Traceback (most recent call last): File "setup.py", line 8, in <module> python_requires='>=2.7', File "/Users/project/tmp/env/lib/python2.7/site-packages/setuptools/__init__.py", line 145, in setup return distutils.core.setup(*

Python 中如何实现参数化测试?

旧时模样 提交于 2019-12-09 22:34:25
之前,我曾转过一个单元测试框架系列的文章,里面介绍了 unittest、nose/nose2 与 pytest 这三个最受人欢迎的 Python 测试框架。 本文想针对测试中一种很常见的测试场景,即参数化测试,继续聊聊关于测试的话题,并尝试将这几个测试框架串联起来,做一个横向的比对,加深理解。 1、什么是参数化测试? 对于普通测试来说,一个测试方法只需要运行一遍,而参数化测试对于一个测试方法,可能需要传入一系列参数,然后进行多次测试。 比如,我们要测试某个系统的登录功能,就可能要分别传入不同的用户名与密码,进行测试:使用包含非法字符的用户名、使用未注册的用户名、使用超长的用户名、使用错误的密码、使用合理的数据等等。 参数化测试是一种“数据驱动测试”(Data-Driven Test),在同一个方法上测试不同的参数,以覆盖所有可能的预期分支的结果。它的测试数据可以与测试行为分离,被放入文件、数据库或者外部介质中,再由测试程序读取。 2、参数化测试的实现思路? 通常而言,一个测试方法就是一个最小的测试单元,其功能应该尽量地原子化和单一化。 先来看看两种实现参数化测试的思路:一种是写一个测试方法,在其内部对所有测试参数进行遍历;另一种是在测试方法之外写遍历参数的逻辑,然后依次调用该测试方法。 这两种思路都能达到测试目的,在简单业务中,没有毛病。然而,实际上它们都只有一个测试单元

[Python]使用pytest进行单元测试

♀尐吖头ヾ 提交于 2019-12-09 13:57:24
安装pytest pipenv install pytest 验证安装的版本: pytest --version This is pytest version 5.3.1, imported from /home/wangju/.virtualenvs/demo_pytest-0JOM2vOx/lib/python3.6/site-packages/pytest.py 接下来通过,几个实例熟悉pytest的用法 实例1: 新建1个py文件,内容如下: 只运行1个case 注意:py文件要以test_开头,否则运行pytest不会运行py文件中的测试方法 import pytest def func(x): return x+1 def test_func(): assert func(3) ==5 执行测试: 执行测试的时候,我们只需要在测试文件test_demo1所在的目录下,运行pytest即可。pytest会在当前的目录下,寻找以test开头的文件(即测试文件),找到测试文件之后,进入到测试文件中寻找test_开头的测试函数并执行。 效果: 分析说明: 标记1处使用命令pytest运行测试 标记2处可以看出断言失败的原因:assert 4==5失败了 实例2: 在终端执行命令运行pytest: pytest -q test_demo2.py 效果: 分析说明: 标记1处: