Skip to content

Commit 0177be8

Browse files
committed
feat: Support reading command line args from a file
1 parent 986434a commit 0177be8

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ Kyle Altendorf
235235
Lawrence Mitchell
236236
Lee Kamentsky
237237
Lev Maximov
238+
Levon Saldamli
238239
Lewis Cowles
239240
Llandy Riveron Del Risco
240241
Loic Esteve

changelog/11871.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Added support for reading ``file_or_dir`` positional arguments from a file
2+
using the prefix character '@', like e.g.:
3+
4+
``pytest @tests.txt``

src/_pytest/config/argparsing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ def __init__(
415415
add_help=False,
416416
formatter_class=DropShorterLongHelpFormatter,
417417
allow_abbrev=False,
418+
fromfile_prefix_chars="@",
418419
)
419420
# extra_info is a dict of (param -> value) to display if there's
420421
# an usage error to provide more contextual information to the user.

testing/acceptance_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import dataclasses
33
import importlib.metadata
44
import os
5+
from pathlib import Path
56
import subprocess
67
import sys
78
import types
@@ -541,6 +542,28 @@ def test_foo(data):
541542
res = pytester.runpytest(p)
542543
res.assert_outcomes(passed=3)
543544

545+
@pytest.mark.filterwarnings("ignore:'encoding' argument not specified")
546+
def test_command_line_args_from_file(
547+
self, pytester: Pytester, tmp_path: Path
548+
) -> None:
549+
pytester.makepyfile(
550+
test_file="""
551+
import pytest
552+
553+
class TestClass:
554+
@pytest.mark.parametrize("a", ["x","y"])
555+
def test_func(self, a):
556+
pass
557+
"""
558+
)
559+
tests = [
560+
"test_file.py::TestClass::test_func[x]",
561+
"test_file.py::TestClass::test_func[y]",
562+
]
563+
args_file = pytester.maketxtfile(tests="\n".join(tests))
564+
result = pytester.runpytest(f"@{args_file.absolute()}")
565+
result.assert_outcomes(failed=0, passed=2)
566+
544567

545568
class TestInvocationVariants:
546569
def test_earlyinit(self, pytester: Pytester) -> None:

testing/test_parseopt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ def test_parse2(self, parser: parseopt.Parser) -> None:
125125
args = parser.parse([Path(".")])
126126
assert getattr(args, parseopt.FILE_OR_DIR)[0] == "."
127127

128+
@pytest.mark.filterwarnings("ignore:'encoding' argument not specified")
129+
def test_parse_from_file(self, parser: parseopt.Parser, tmp_path: Path) -> None:
130+
tests = [".", "some.py::Test::test_method[param0]", "other/test_file.py"]
131+
args_file = tmp_path / "tests.txt"
132+
args_file.write_text("\n".join(tests), encoding="utf-8")
133+
args = parser.parse([f"@{args_file.absolute()}"])
134+
assert getattr(args, parseopt.FILE_OR_DIR) == tests
135+
128136
def test_parse_known_args(self, parser: parseopt.Parser) -> None:
129137
parser.parse_known_args([Path(".")])
130138
parser.addoption("--hello", action="store_true")

0 commit comments

Comments
 (0)