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

witty-laptop-37343

03/07/2023, 11:03 PM
Hi team, is there a way that we can run pants with multiple pytest arguments? According to the official document, I need to use “--” to pass the arguments to pytest. If I try it with only one argument, it works. But with multiple arguments, it failed… for example:
Copy code
./pants test t.py -- --name "Ben" --age 30  --year 2017
?
l

lemon-helicopter-73409

03/07/2023, 11:10 PM
If this isn’t right (haven’t been playing with pants too long), please correct me! That said, this is what I have in my
pants.toml
file:
Copy code
[pytest]
lockfile = ".build-support/pytest.lockfile"
args = [
    "-ra -q -vv --no-header",
    "--reuse-db",
    "--no-migrations",
]
@witty-laptop-37343 ^
w

witty-laptop-37343

03/07/2023, 11:11 PM
is it possible that I don;t have it hard coded?
l

lemon-helicopter-73409

03/07/2023, 11:12 PM
Ah, for that, someone else will have to answer
w

witty-laptop-37343

03/07/2023, 11:12 PM
I would like to be able to run test and provide args in one single command
any advice on this?
h

high-yak-85899

03/07/2023, 11:14 PM
What is not working about your arguments above? Whatever works with
pytest <args>
should work with
./pants test <module> -- <args>
w

witty-laptop-37343

03/07/2023, 11:19 PM
Copy code
ERROR: usage: pex [options] [file_or_dir] [file_or_dir] [...]
pex: error: unrecognized arguments: --name
any help on this?😭 or has anyone managed to run pants pytest multiple arguments?
w

witty-crayon-22786

03/07/2023, 11:59 PM
i use multiple pytest arguments fairly commonly:
Copy code
./pants test $file -- -k $testname -vvv
…for example.
it looks like you are not actually passing arguments that would be understood by
pytest
though. what defines “--age” and “--name”?
and what is the actual error that you get with your original snippet?
finally, what version of Pants is this?
e

enough-analyst-54434

03/08/2023, 12:04 AM
Works fine:
Copy code
$ pants test --output=all src/python/pants/util/strutil_test.py -- -vvs -k test_bullet_list
16:04:00.62 [INFO] Completed: Run Pytest - src/python/pants/util/strutil_test.py:tests - succeeded.
============================= test session starts ==============================
collecting ... collected 18 items / 16 deselected / 2 selected

src/python/pants/util/strutil_test.py::test_bullet_list PASSED
src/python/pants/util/strutil_test.py::test_bullet_list_max_elements PASSED

- generated xml file: /tmp/pants-sandbox-aolMiY/src.python.pants.util.strutil_test.py.tests.xml -
======================= 2 passed, 16 deselected in 0.18s =======================



✓ src/python/pants/util/strutil_test.py:tests succeeded in 0.45s.
I also use that alot / have done so for a long time. The above is Pants main though.
I suspect --age and --name are fielded by pytest plugins not getting loaded.
w

witty-laptop-37343

03/08/2023, 12:35 AM
I used conftest, pytest_addoption to load the arguments
Copy code
def pytest_addoption(parser):
    parser.addoption('--name', action='store', default=None)
    parser.addoption('--age', action='store', default=20)
then I use fixture
Copy code
@pytest.fixture(scope='session')
def name(pytestconfig):
    return pytestconfig.getoption('name')
when I run pytest test_data.py --name “Ben” --age 30 --year 2017 it works
e

enough-analyst-54434

03/08/2023, 12:41 AM
Do you have any proof the conftest.py is being loaded? You need a
python_test_utils
target owning that file. Do you have that? https://www.pantsbuild.org/docs/reference-python_test_utils
w

witty-laptop-37343

03/08/2023, 12:42 AM
does
Copy code
./pants test <test_file_name> -- --arg1 val1 --arg2 val2
work for you?
e

enough-analyst-54434

03/08/2023, 12:42 AM
Above I show it does.
w

witty-laptop-37343

03/08/2023, 12:43 AM
what’s -k and --vvs option?😅
e

enough-analyst-54434

03/08/2023, 12:43 AM
Those are pytest options.
Try
pants test path/to/file -- --help
That will dump pytest help, you're talking to pytest at that point.
w

witty-laptop-37343

03/08/2023, 12:46 AM
yep, I see my pytest help options
e

enough-analyst-54434

03/08/2023, 12:46 AM
Ok, so you'll just need to dig.
It's Python luckily; so you can add prints anywhere.
😅 1
I'm serious there. If I were to figure the rest out for you, that's what I would be doing.
w

witty-laptop-37343

03/08/2023, 12:48 AM
yep, I understand, let me try that
thanks!😁
the weird thing is that when I only try with any ONE of my argument, it works, for example
Copy code
./pants test t.py -- --name "Ben"
or
Copy code
./pants test t.py -- --age 20
h

happy-kitchen-89482

03/08/2023, 12:57 AM
What happens if you surround all those args in quotes?
./pants test t.py -- '--name "Ben" --age 20'
w

witty-laptop-37343

03/08/2023, 1:30 AM
it gives me
Copy code
Error: file or directory not found: --name "Ben" --age 20
h

happy-kitchen-89482

03/08/2023, 4:05 AM
Just an idea. OK, so, back to exploring as John suggested.
If you run with
--keep-sandboxes=always
Pants will preserve (and log) the sandbox it used to run pytest in, and
__run.sh
in the sandbox will run the command again, so you can play with that
w

witty-laptop-37343

03/09/2023, 12:20 AM
thank you everyone for helping, regarding to passing multiple arguments, it turns out the correct syntax is ./pants test t.py -- --name=“Ben” --age=20
h

happy-kitchen-89482

03/09/2023, 5:24 AM
Good to know, but to be very clear, everything after
--
is passed through to pytest, so this "correct syntax" is a pytest/pytest plugin thing, not a Pants thing
5 Views