Hi team, is there a way that we can run pants with...
# general
w
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
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
is it possible that I don;t have it hard coded?
l
Ah, for that, someone else will have to answer
w
I would like to be able to run test and provide args in one single command
any advice on this?
h
What is not working about your arguments above? Whatever works with
pytest <args>
should work with
./pants test <module> -- <args>
w
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
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
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
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
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
does
Copy code
./pants test <test_file_name> -- --arg1 val1 --arg2 val2
work for you?
e
Above I show it does.
w
what’s -k and --vvs option?😅
e
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
yep, I see my pytest help options
e
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
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
What happens if you surround all those args in quotes?
./pants test t.py -- '--name "Ben" --age 20'
w
it gives me
Copy code
Error: file or directory not found: --name "Ben" --age 20
h
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
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
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