Discussion:
[TIP] Struggling with nose-to-pytest config file
Skip Montanaro
2018-05-01 13:52:58 UTC
Permalink
I'm moving from nose to pytest and am struggling a bit to get a
working/useful config file. I renamed .noserc to pytest.ini and didn't
change anything. Things seemed to work okay, but various bits of
functionality are missing (output's not verbose, doctests aren't run,
xunit/junit output not generated). I was a bit worried that it was ignoring
my config file (even though it reports finding it).

Here's one basic example. When I use --verbose on the command line, I get
the expected verbose output, one line per test. If I give

[pytest]
verbose=yes

in the ini file, I get no verbose output, just the one dot per test. I also
tried verbosity=1 and verbosity=2 to no avail. In addition, none of those
three settings provoked an error message from pytest. Pytest never
complains about anything. It seems to silently ignore everything.

In general, it's not clear how I do the following:

1. Translate old .noserc settings into pytest-speak

2. Move settings off the command line into the config file. (I see the
addopts thing, but aren't there equivalent configuration settings for the
command line flags?)

Google failed me. I saw some blog posts about converting from nose to
pytest, but didn't see any discussion about the translation of
configuration settings. Does pytest have some kind of magic
"--generate-config" flag which will spit out a pytest.ini file with all the
default values?

Thanks,

Skip Montanaro
Bruno Oliveira
2018-05-01 14:38:31 UTC
Permalink
Hi Skip,
Post by Skip Montanaro
I'm moving from nose to pytest and am struggling a bit to get a
working/useful config file. I renamed .noserc to pytest.ini and didn't
change anything. Things seemed to work okay, but various bits of
functionality are missing (output's not verbose, doctests aren't run,
xunit/junit output not generated). I was a bit worried that it was ignoring
my config file (even though it reports finding it).
Here's one basic example. When I use --verbose on the command line, I get
the expected verbose output, one line per test. If I give
[pytest]
verbose=yes
in the ini file, I get no verbose output, just the one dot per test. I also
tried verbosity=1 and verbosity=2 to no avail. In addition, none of those
three settings provoked an error message from pytest. Pytest never
complains about anything. It seems to silently ignore everything.
1. Translate old .noserc settings into pytest-speak
2. Move settings off the command line into the config file. (I see the
addopts thing, but aren't there equivalent configuration settings for the
command line flags?)
You are correct, the proper way is to use `addopts` to always pass some
options to pytest as if they were typed in the command line. From that you
said at the start it seems this is a good start:

[pytest]
addopts = -v --doctest-modules --junitxml=tests.xml

As a rule pytest doesn't mimic all command-line options as a separate ini
setting, as it seems to be the case with nose.

You can see all ini settings in the reference docs:
https://docs.pytest.org/en/latest/reference.html#configuration-options
Post by Skip Montanaro
Google failed me. I saw some blog posts about converting from nose to
pytest, but didn't see any discussion about the translation of
configuration settings. Does pytest have some kind of magic
"--generate-config" flag which will spit out a pytest.ini file with all the
default values?
It doesn't currently, but I think such a flag could indeed be useful.

Right now it also doesn't display any message about unknown settings; I
think that is because since plugins can add new config settings, it might
happen that an user doesn't have a plugin installed but their pytest.ini
contains settings which are added by the missing plugin, so issuing an
error in that situation would be a problem. But a warning surely would be
welcome instead.

Cheers,
Bruno.
Skip Montanaro
2018-05-01 16:18:54 UTC
Permalink
Thanks for the response, Bruno. I seem to be back in business.

I'm not familiar with the code, and with a quick skim nothing jumped out at
me. Is it possible to include environment variable references in the
addopts option? For example, is this valid?

[pytest]
addopts = --junit-xml=${XUNIT_DIR}/xunit.xml

At the moment I can get by without it, but envision needing something like
it in the not-too-distant future.

Thx,

Skip
Bruno Oliveira
2018-05-01 16:41:20 UTC
Permalink
Hi Skip,
Post by Skip Montanaro
I'm not familiar with the code, and with a quick skim nothing jumped out at
me. Is it possible to include environment variable references in the
addopts option? For example, is this valid?
[pytest]
addopts = --junit-xml=${XUNIT_DIR}/xunit.xml
Yes this works, but because the junitxml plugin expands environment
variables explicitly[1], it is not something done automatically for all ini
settings.

[1]
https://github.com/pytest-dev/pytest/blob/08aed1a6bf90e2b8641aa464b41a41f1e400b59e/_pytest/junitxml.py#L344

Cheers,
Bruno

Loading...