https://pantsbuild.org/ logo
#general
Title
# general
r

rhythmic-glass-66959

07/18/2022, 6:58 PM
About pytest, I know that we can install a plugin for certain tests (using the
dependencies
field in
python_test
target), but can we do the same with arguments?
h

happy-kitchen-89482

07/18/2022, 7:17 PM
cli args to pass through to pytest?
You can pass args through to all tests in a run with
./pants test <targets> -- <pytest args"
but not per-test
possibly you can use conftest.py to achieve what you need?
that is a standard pytest customization mechanism that Pants knows about and makes use of
h

hundreds-father-404

07/18/2022, 7:31 PM
Yeah this came up last week. Someone worked around it by using conftest.py But I remain open to us adding a field like
extra_cli_args
to the
python_test
target! I think it may be worth doing because it allows you more precision than
conftest.py
, which is directory based Super easy Fairly trivial to add if you have any interest? I'd be happy to write instructions 🙂
r

rhythmic-glass-66959

07/18/2022, 8:21 PM
I think it would be useful. For example, we need to tell pytest-django which Django settings should be used for test runs. To achieve this, you can set
DJANGO_SETTINGS_MODULE
in pytest.ini (or I guess in pants.toml) or use the
--ds
command line flag. The problem is that not all our projects are Django apps. Of course, since I'm new to pants, maybe there's a better way...
Thinking about it, maybe my original question should be more like: Can I use a
pytest.ini
per-test?
h

hundreds-father-404

07/18/2022, 8:28 PM
Can I use a pytest.ini per-test?
No. The closest is using
conftest.py
, which inherits Pytest's semantics. It applies for all files in the dir and subdirs
But we're totally open to adding an
extra_cli_args
field to
python_test
, which would give you per-file control
r

rhythmic-glass-66959

07/18/2022, 8:35 PM
That would be great, but I'm not sure that all the settings have a command line option... For example
django_find_project
.
A little heads up: we finally have to define a pytest bootstrapping hook
pytest_load_initial_conftests
since Nautobot does not use
DJANGO_SETTINGS_MODULE
to load its configuration. As per the documentation, this hook will not be called for
conftest.py
files, only for setuptools plugins. To call a setuptools plugin, you need to use the
-p
argument on the command line. So using
conftest.py
as suggested won't work in this case and adding a field like
extra_cli_args
would be really helpful here. In the meantime, I found a workaround using
extra_env_vars
on the
python_tests
target like this:
Copy code
python_tests(
    name="tests",
    dependencies=[
        ...,
        "src/python/pytest_nautobot",
    ],
    extra_env_vars=[
        ...,
        "PYTEST_ADDOPTS=-p pytest_nautobot"
    ]
)
Since I'm new to pants, let me know if this make sense or if there's a better way to pass pytest arguments to only a subset of tests.
h

happy-kitchen-89482

07/21/2022, 2:34 AM
At first glance that workaround seems very sensible to me
🙏 1
5 Views