Missing test coverage pytest python class

3.3k Views Asked by At

I am testing my code with pytest --cov but one of my modules gets 0% coverage.

The module has one class declaration as such:

class DataBaseMaker:
    @staticmethod
    def create_database():
        conn = sqlite3.connect("database.db")
        
    def __init__(self):
        self.foo = 'bar'
        

The test does the following:

def test_create_database():
    DataBaseMaker.create_database()
    assert Path("database.db").is_file()

Test coverage for this is 0% - what am I doing wrong here?

1

There are 1 best solutions below

3
On BEST ANSWER

I was able to find out what the issue is. It was not with the code itself but with my invocation of pytest. I did pytest --cov *projectname* where projectname was also the name of the folder the project is in. I think I got this from the pytest documentation? Not sure.

The solution: I ran pytest --cov . and sure enough my classes have 100% coverage now. If someone has an explanation for that behavior I'd be happy to learn.