'How do I fix PytestAssertRewriteWarning?

I'm just starting out with pytest and pytest_mysql and have the following code:

import pytest
from pytest_mysql import factories

if __name__ == "__main__":
    pytest.main([r'test.py', '-v'])
    testdb = factories.mysql('testdb')

This generates a warning:

PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_mysql
  1. Should I be concerned?
  2. How can I ensure I don't get the warning?


Solution 1:[1]

This is how i solved it!

import pytest
if __name__=='__main__': #Note 1
    pytest.main()
#
#Your code goes here
#
from pytest_mysql import factories
testdb = factories.mysql('testdb')

So try running pytest.main() before you import anything else..

Note 1

That's not the usual way you call __name__=='__main__', but as it works i buy it!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1