yars
yars

Reputation: 43

Write unit test for checking code coverage in python

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

Answers (1)

Ned Batchelder
Ned Batchelder

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

Related Questions