Skip to content

Commit 8636cf6

Browse files
Add --no-output-structure flag to pytest 🐍 (#162)
* src/pytest_plugins/test_filler: Add --no-output-structure flag. * src/pytest_plugins/test_filler: create top-level output dir * src/pytest_plugins/test_filler: Change to --flat-output flag. --------- Co-authored-by: danceratopz <[email protected]>
1 parent 16461db commit 8636cf6

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/pytest_plugins/test_filler/test_filler.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def pytest_addoption(parser):
7979
default="./fixtures/",
8080
help="Directory to store the generated test fixtures. Can be deleted.",
8181
)
82+
test_group.addoption(
83+
"--flat-output",
84+
action="store_true",
85+
dest="flat_output",
86+
default=False,
87+
help="Output each test case in the directory without the folder structure.",
88+
)
8289

8390

8491
@pytest.hookimpl(tryfirst=True)
@@ -172,10 +179,12 @@ class FixtureCollector:
172179

173180
all_fixtures: Dict[str, List[Tuple[str, Any]]]
174181
output_dir: str
182+
flat_output: bool
175183

176-
def __init__(self, output_dir: str) -> None:
184+
def __init__(self, output_dir: str, flat_output: bool) -> None:
177185
self.all_fixtures = {}
178186
self.output_dir = output_dir
187+
self.flat_output = flat_output
179188

180189
def add_fixture(self, item, fixture: Fixture) -> None:
181190
"""
@@ -196,9 +205,13 @@ def get_module_dir(item) -> str:
196205
)
197206
return module_dir
198207

199-
module_dir = os.path.join(
200-
get_module_dir(item),
201-
strip_test_prefix(item.originalname),
208+
module_dir = (
209+
strip_test_prefix(item.originalname)
210+
if self.flat_output
211+
else os.path.join(
212+
get_module_dir(item),
213+
strip_test_prefix(item.originalname),
214+
)
202215
)
203216
if module_dir not in self.all_fixtures:
204217
self.all_fixtures[module_dir] = []
@@ -215,14 +228,16 @@ def dump_fixtures(self) -> None:
215228
"""
216229
Dumps all collected fixtures to their respective files.
217230
"""
231+
os.makedirs(self.output_dir, exist_ok=True)
218232
for module_file, fixtures in self.all_fixtures.items():
219233
output_json = {}
220234
for index, name_fixture in enumerate(fixtures):
221235
name, fixture = name_fixture
222236
name = str(index).zfill(3) + "-" + name
223237
output_json[name] = fixture
224238
file_path = os.path.join(self.output_dir, module_file + ".json")
225-
os.makedirs(os.path.dirname(file_path), exist_ok=True)
239+
if not self.flat_output:
240+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
226241
with open(file_path, "w") as f:
227242
json.dump(output_json, f, indent=4)
228243

@@ -233,7 +248,10 @@ def fixture_collector(request):
233248
Returns the configured fixture collector instance used for all tests
234249
in one test module.
235250
"""
236-
fixture_collector = FixtureCollector(output_dir=request.config.getoption("output"))
251+
fixture_collector = FixtureCollector(
252+
output_dir=request.config.getoption("output"),
253+
flat_output=request.config.getoption("flat_output"),
254+
)
237255
yield fixture_collector
238256
fixture_collector.dump_fixtures()
239257

0 commit comments

Comments
 (0)