Reputation: 43
I need to write test using unittest in python that fails when coverage is less than 50 percent. Such as:
class ExampleTest(unittest.TestCase):
def setUp(self):
cov = coverage.Coverage()
cov.load()
with open(os.devnull, "w") as f:
self.total = cov.report(file=f)
def test_compare_values(self):
self.assertGreaterEqual(self.total, 20)
But when tests are running the file coverage is locked and cannot be opened.
How solve this problem?
Upvotes: 2
Views: 729
Reputation: 375574
Don't try to read the coverage data from a test. Instead use the --fail-under=50
option on the coverage report command.
Upvotes: 1