Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple arguments in pytest using command line?

I want to pass inputs to my pytest file as a command line option. This question https://stackoverflow.com/a/42145604/8031479 was helpful but I don't know to add multiple parser adoptions.

I have tried adding this to my conftest.py file, but was not helpful:

def pytest_addoption(parser):
    """
        This function is used to extract input1 and input2 values from the command line
    """
    parser.addoption(
        "--input1", action="store", default="input1"
    )
    parser.addoption(
        "--input2", action="store", default="input2"
    )

Contents of my test.py file:

import pytest

@pytest.fixture()
def get_input1(input1):
    print 'input1:', input1
    return input1

# @pytest.mark.unit
@pytest.fixture()
def get_input2(input2):
    print 'input2:', input2
    return input2

def test_hello(get_input1, get_input2):
    print 'testing pytest fixtures with command line options'
    print get_input1, get_input2

This is my command to run the test.py file:

py.test test.py --input1="hello" --input2="world"

I am getting this error message:

@pytest.fixture()
def get_input1(input1):
E       fixture 'input1' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, get_input1, get_input2, metadata, monkeypatch, pytestconfig, record_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.
like image 704
Repakula Srushith Avatar asked Jan 29 '19 08:01

Repakula Srushith


People also ask

Can pytest fixtures have arguments?

You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest. mark. parametrize decorator for individual tests.

What is the syntax to run a parameterized test in pytest?

you can put @pytest. mark. parametrize style parametrization on the test functions to parametrize input/output values as well.


1 Answers

You can make it work this way:

conftest.py:

import pytest


def pytest_addoption(parser):
    parser.addoption("--input1", action="store", default="default input1")
    parser.addoption("--input2", action="store", default="default input2")



@pytest.fixture
def input1(request):
    return request.config.getoption("--input1")


@pytest.fixture
def input2(request):
    return request.config.getoption("--input2")

test.py:

import pytest

@pytest.mark.unit
def test_print_name(input1, input2):
    print ("Displaying input1: %s" % input1)
    print("Displaying input2: %s" % input2)

CLI:

>py.test -s test.py --input1 tt --input2 12
================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: pytest, inifile:
collected 1 item

test.py Displaying input1: tt
Displaying input2: 12
.

============================================== 1 passed in 0.04 seconds ===============================================
like image 81
Adonis Avatar answered Oct 22 '22 06:10

Adonis