I have the below project structure
- root
- main.py
- test/
- __init__.py
- conftest.py
- test.py
In conftest.py
I have some custom fixtures using mysql.connector
. Therefore I am importing:
import mysql.connector
@pytest.fixture(scope='module') # maintain connection for all tests
def cnx(database, username, password):
cnx = mysql.connector.connect(database=database, user=username, password=password)
yield cnx
cnx.close()
when I run test.py
from the terminal in the root directory with the below command:
pytest tests/test.py
I get the below error:
ImportError while loading conftest '/tests/conftest.py'.
tests/conftest.py:5: in <module>
import mysql.connector
E ModuleNotFoundError: No module named 'mysql'
Why is this happening? mysql
is definitely installed.
The command to run a test script with
pytest
is notpytest tests/test.py
butas detailed in the official docs.