Skip to content

Commit 5b7fef5

Browse files
committed
Added dir. config for report, Updated README.md
1 parent 4a6c06b commit 5b7fef5

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ myproject/module2.py 10 3 70% func3, func4, <lambda>
5050
-------------------------------------------------------
5151
TOTAL 17 8 47%
5252
```
53-
53+
Also ```json``` option can be used with ```--func_cov_report```. To get the output in a JSON file
54+
```bash
55+
pytest --func_cov=myproject --func_cov_report=json
56+
```
5457
# Configuration
5558
A list of function name patterns to ignore can be specified in pytest.ini.
5659

@@ -61,4 +64,12 @@ ignore_func_names =
6164
^test_*
6265
^myfunction$
6366
```
64-
This will ignore all function names starting with "test_" and functions named "myfunction".
67+
This will ignore all function names starting with "test_" and functions named "myfunction".
68+
69+
You can specify the directory to save the function coverage report.
70+
71+
Example
72+
```ini
73+
[pytest]
74+
json_path = public
75+
```

pytest_func_cov/plugin.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import sys
33
import json
4-
54
from .tracking import FunctionIndexer, get_full_function_name
65

76

@@ -35,6 +34,7 @@ def pytest_addoption(parser):
3534
)
3635

3736
parser.addini("ignore_func_names", "function names to ignore", "linelist", [])
37+
parser.addini("json_path", "path for saving json file", "linelist", [])
3838

3939

4040
def pytest_load_initial_conftests(early_config, parser, args):
@@ -47,6 +47,7 @@ class FuncCovPlugin:
4747
def __init__(self, args):
4848
self.args = args
4949
self.indexer = FunctionIndexer(args.getini("ignore_func_names"))
50+
self.json_path = args.getini("json_path")
5051

5152
def pytest_sessionstart(self, session):
5253
"""
@@ -153,14 +154,23 @@ def pytest_terminal_summary(self, terminalreporter):
153154
total_cover = 0
154155

155156
args = ("TOTAL", total_funcs, total_miss, total_cover)
157+
156158
if include_json:
157159
report_data = {
158160
"total_funcs": total_funcs,
159161
"total_miss": total_miss,
160162
"total_cover": total_cover,
161163
}
162-
with open("func_report.json", "w") as json_file:
163-
json.dump(report_data, json_file, indent=4)
164+
165+
try:
166+
file_path = os.path.join(self.json_path[0], "func_report.json")
167+
os.makedirs(self.json_path[0], exist_ok=True)
168+
with open(file_path, "w") as json_file:
169+
json.dump(report_data, json_file, indent=4)
170+
except IndexError:
171+
with open("func_report.json", "w") as json_file:
172+
json.dump(report_data, json_file, indent=4)
173+
164174
if include_missing:
165175
args += ("",)
166176

tests/test_tracking.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def nested_package_directory():
9999
Returns
100100
Tuple[str, Tuple[Tuple[str, str], ...]]: First element is the absolute path
101101
to the created directory, and the second is a tuple of tuples, where the first
102-
element is the absolute path to a .py file, and the second one is the name under
102+
element is the absolute path to a .py file, and the second one is the name under
103103
which is should be imported.
104104
"""
105105
with tempfile.TemporaryDirectory() as package_folder:

0 commit comments

Comments
 (0)