-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpytest_helper.py
More file actions
31 lines (28 loc) · 932 Bytes
/
pytest_helper.py
File metadata and controls
31 lines (28 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Helper methods for using Pytest within Bazel."""
import os
import sys
import pytest
try:
import coverage
COVERAGE = True
except ImportError:
COVERAGE = False
if COVERAGE:
# We need to do this here, otherwise it won't catch method/class
# declarations. Also, helpers imports should be before all other local
# imports.
cov_file = "%s/coverage.cov" % os.environ["TEST_UNDECLARED_OUTPUTS_DIR"]
cov = coverage.Coverage(data_file=cov_file)
cov.start()
def main(script_name, file_name):
"""Test runner that supports Bazel test and the coverage_report.sh script.
Tests should import this module before importing any other local scripts,
then call main(__name__, __file__) after declaring their tests.
"""
if script_name != "__main__":
return
exit_code = pytest.main([file_name, "-s"])
if COVERAGE:
cov.stop()
cov.save()
sys.exit(exit_code)