feat: Support reading command line args from a file

This commit is contained in:
Levon Saldamli 2024-03-07 00:06:51 +01:00
parent 1a33aee2c9
commit 590a807371
No known key found for this signature in database
5 changed files with 37 additions and 0 deletions

View File

@ -235,6 +235,7 @@ Kyle Altendorf
Lawrence Mitchell
Lee Kamentsky
Lev Maximov
Levon Saldamli
Lewis Cowles
Llandy Riveron Del Risco
Loic Esteve

View File

@ -0,0 +1,4 @@
Added support for reading ``file_or_dir`` positional arguments from a file
using the prefix character '@', like e.g.:
``pytest @tests.txt``

View File

@ -415,6 +415,7 @@ class MyOptionParser(argparse.ArgumentParser):
add_help=False,
formatter_class=DropShorterLongHelpFormatter,
allow_abbrev=False,
fromfile_prefix_chars="@",
)
# extra_info is a dict of (param -> value) to display if there's
# an usage error to provide more contextual information to the user.

View File

@ -2,6 +2,7 @@
import dataclasses
import importlib.metadata
import os
from pathlib import Path
import subprocess
import sys
import types
@ -541,6 +542,28 @@ class TestGeneralUsage:
res = pytester.runpytest(p)
res.assert_outcomes(passed=3)
@pytest.mark.filterwarnings("ignore:'encoding' argument not specified")
def test_command_line_args_from_file(
self, pytester: Pytester, tmp_path: Path
) -> None:
pytester.makepyfile(
test_file="""
import pytest
class TestClass:
@pytest.mark.parametrize("a", ["x","y"])
def test_func(self, a):
pass
"""
)
tests = [
"test_file.py::TestClass::test_func[x]",
"test_file.py::TestClass::test_func[y]",
]
args_file = pytester.maketxtfile(tests="\n".join(tests))
result = pytester.runpytest(f"@{args_file.absolute()}")
result.assert_outcomes(failed=0, passed=2)
class TestInvocationVariants:
def test_earlyinit(self, pytester: Pytester) -> None:

View File

@ -125,6 +125,14 @@ class TestParser:
args = parser.parse([Path(".")])
assert getattr(args, parseopt.FILE_OR_DIR)[0] == "."
@pytest.mark.filterwarnings("ignore:'encoding' argument not specified")
def test_parse_from_file(self, parser: parseopt.Parser, tmp_path: Path) -> None:
tests = [".", "some.py::Test::test_method[param0]", "other/test_file.py"]
args_file = tmp_path / "tests.txt"
args_file.write_text("\n".join(tests), encoding="utf-8")
args = parser.parse([f"@{args_file.absolute()}"])
assert getattr(args, parseopt.FILE_OR_DIR) == tests
def test_parse_known_args(self, parser: parseopt.Parser) -> None:
parser.parse_known_args([Path(".")])
parser.addoption("--hello", action="store_true")