nikhilesh_koshti
nikhilesh_koshti

Reputation: 403

Issue in pytest_generate_tests() in pytest

"conftest.py"

def pytest_addoption(parser):
    parser.addoption("--stringinput", action= "append", default = ["defaultstring"], help = "Input String")

def pytest_generate_tests(metafunc):
    if "stringinput" in metafunc.fixturenames:
         metafunc.parametrize("stringinput",metafunc.config.getoption("stringinput"))

when we execute this code with passing the command line option "stringinput" as "pytest pytest_generate_test_example.py --stringinput="hello" ,it also execute the default values in the output. The snippet of the ouput is as follows:--

pytest_generate_test_example.py::test_valid_string[defaultstring] PASSEDpytest_generate_test_example.py::test_valid_string[hello] PASSED

Upvotes: 0

Views: 126

Answers (1)

phd
phd

Reputation: 95038

This is argparse's "problem" not related to pytest. See the test script:

#! /usr/bin/env python

import argparse

parser = argparse.ArgumentParser(description='Import')
parser.add_argument('test', action='append', default=['foo'])
args = parser.parse_args('bar'.split())

print args.test

It outputs ['foo', 'bar']. I.e. action='append' appends to the default without clearing it first.

So to avoid the problem start with an empty default list and use your defaultstring if the argument is empty (there was no --stringinput command line parameter).

Upvotes: 1

Related Questions