Using pytestconfig

With the pytestconfig builtin fixture, you can control how pytest runs through command-line arguments and options, configuration files, plugins, and the directory from which you launched pytest. The pytestconfig fixture is a shortcut to request.config, and is sometimes referred to in the pytest documentation as “the pytest config object.”

To see how pytestconfig works, you’ll look at how to add a custom command-line option and read the option value from within a test. You can read the value of command-line options directly from pytestconfig, but to add the option and have pytest parse it, you need to add a hook function. Hook functions, which I cover in more detail in Chapter 5, Plugins, are another way to control how pytest behaves and are used frequently in plugins. However, adding a custom command-line option and reading it from pytestconfig is common enough that I want to cover it here.

We’ll use the pytest hook pytest_addoption to add a couple of options to the options already available in the pytest command line:

 def​ pytest_addoption(parser):
  parser.addoption(​"--myopt"​, action=​"store_true"​,
  help=​"some boolean option"​)
  parser.addoption(​"--foo"​, action=​"store"​, default=​"bar"​,
  help=​"foo: bar or baz"​)

Adding command-line options via pytest_addoption should be done via plugins or in the conftest.py file at the top of your project directory structure. You shouldn’t do it in a test subdirectory.

The options --myopt and --foo <value> were added to the previous code, and the help string was modified, as shown here:

 $ ​​cd​​ ​​/path/to/code/ch4/pytestconfig
 $ ​​pytest​​ ​​--help
 usage: pytest [options] [file_or_dir] [file_or_dir] [...]
 ...
 custom options:
  --myopt some boolean option
  --foo=FOO foo: bar or baz
 ...

Now we can access those options from a test:

 import​ pytest
 
 
 def​ test_option(pytestconfig):
 print​(​'"foo" set to:'​, pytestconfig.getoption(​'foo'​))
 print​(​'"myopt" set to:'​, pytestconfig.getoption(​'myopt'​))

Let’s see how this works:

 $ ​​pytest​​ ​​-s​​ ​​-q​​ ​​test_config.py::test_option
 "foo" set to: bar
 "myopt" set to: False
 .
 1 passed in 0.01 seconds
 $ ​​pytest​​ ​​-s​​ ​​-q​​ ​​--myopt​​ ​​test_config.py::test_option
 "foo" set to: bar
 "myopt" set to: True
 .
 1 passed in 0.01 seconds
 $ ​​pytest​​ ​​-s​​ ​​-q​​ ​​--myopt​​ ​​--foo​​ ​​baz​​ ​​test_config.py::test_option
 "foo" set to: baz
 "myopt" set to: True
 .
 1 passed in 0.01 seconds

Because pytestconfig is a fixture, it can also be accessed from other fixtures. You can make fixtures for the option names, if you like, like this:

 @pytest.fixture()
 def​ foo(pytestconfig):
 return​ pytestconfig.option.foo
 
 
 @pytest.fixture()
 def​ myopt(pytestconfig):
 return​ pytestconfig.option.myopt
 
 
 def​ test_fixtures_for_options(foo, myopt):
 print​(​'"foo" set to:'​, foo)
 print​(​'"myopt" set to:'​, myopt)

You can also access builtin options, not just options you add, as well as information about how pytest was started (the directory, the arguments, and so on).

Here’s an example of a few configuration values and options:

 def​ test_pytestconfig(pytestconfig):
 print​(​'args :'​, pytestconfig.args)
 print​(​'inifile :'​, pytestconfig.inifile)
 print​(​'invocation_dir :'​, pytestconfig.invocation_dir)
 print​(​'rootdir :'​, pytestconfig.rootdir)
 print​(​'-k EXPRESSION :'​, pytestconfig.getoption(​'keyword'​))
 print​(​'-v, --verbose :'​, pytestconfig.getoption(​'verbose'​))
 print​(​'-q, --quiet :'​, pytestconfig.getoption(​'quiet'​))
 print​(​'-l, --showlocals:'​, pytestconfig.getoption(​'showlocals'​))
 print​(​'--tb=style :'​, pytestconfig.getoption(​'tbstyle'​))

You’ll use pytestconfig again when I demonstrate ini files in Chapter 6, Configuration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.80.100