I start off with all the code in conftest.py. This is in example 1. In example 2 I want to move a functions like def test_username_db():
into a separate file called test_function.py. Then I want to import from test_function.py into conftest.py. The problem is I am getting an error. How do I fix this?
example 1
conftest.py
Also I wrote the database class here.
@pytest.fixture
def test_username_db():
# assume bob exists
user_db = User.query.filter_by(username='Bob').first()
assert user_db != 'Bob'
# other fixtures...
@pytest.fixture()
def create_db(new_user, test_username_db):
bind_key="testing_app_db"
# Create the database and the database table
db.create_all(bind_key)
db.session.add(new_user)
db.session.commit()
# yield unlike return doesn't stop when called.
yield test_username_db()
yield other fixtures
db.drop_all(bind_key)
test_fixture.py
test_fixture.py(create_db)
create_db()
Example 2
Now imagine I want to split up the extra fixtures into functions in a file called test_function.py.
test_function.py
Also I wrote the database class here.
def test_username_db():
# assume bob exists
user_db = User.query.filter_by(username='Bob').first()
assert user_db != 'Bob'
# other fixtures...
conftest.py
from tests.test_function import test_username_db ...
@pytest.fixture()
def create_db(new_user):
bind_key="testing_app_db"
# Create the database and the database table
db.create_all(bind_key)
db.session.add(new_user)
db.session.commit()
# yield unlike return doesn't stop when called.
yield test_username_db()
yield # other functions
db.drop_all(bind_key)
test_fixture.py
test_fixture.py(create_db)
create_db()
This seems okay but the problem is when I run the code in example 2 I get an error.
Here is the error
python -m pytest
========================================================================================= test session starts ==========================================================================================
platform win32 -- Python 3.10.8, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\Users\user\OneDrive\Desktop\flaskcodeusethis\flaskblog2
collected 0 items / 1 error
================================================================================================ ERRORS ================================================================================================
____________________________________________________________________________________ ERROR collecting test session _____________________________________________________________________________________
..\..\..\..\Anaconda3\envs\py\lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
???
<frozen importlib._bootstrap>:1027: in _find_and_load
???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:688: in _load_unlocked
???
..\..\..\..\Anaconda3\envs\py\lib\site-packages\_pytest\assertion\rewrite.py:168: in exec_module
exec(co, module.__dict__)
app\tests\conftest.py:13: in <module>
from tests.test_password_function import app, db, UserTest, email_token_and_registration_confirmation_email
E ModuleNotFoundError: No module named 'tests'
======================================================================================= short test summary info ========================================================================================
ERROR - ModuleNotFoundError: No module named 'tests'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
My testing folder looks like this tests folder and files . Why is this error occurring?
Also the error I posted I am using the function def email_token_and_registration_confirmation_email():
instead of def test_username_db():
.
Also the error I posted I am using the file test_functions_use_db.py
instead of test_function.py
.
Here is what I tried.
I tried changing the functions test_functions_use_db.py to functions_use_db.py. I get the same effect.
I also found this link Using conftest.py vs. importing fixtures from dedicate modules. I tried adding some functions and test_email_token_and_registration_confirmation_email()
fixture into one file and I am still getting a very similar error.
example 3
In example 3 instead of a function for def test_username_db():
I am using a fixture
test_function.py
Also I wrote the database class here.
import fixture
@pytest.fixture
def test_username_db():
# assume bob exists
user_db = User.query.filter_by(username='Bob').first()
assert user_db != 'Bob'
# other fixtures...
conftest.py
from tests.test_function import test_username_db ...
__all__ = ['test_username_db']
@pytest.fixture()
def create_db(new_user, test_username_db):
bind_key="testing_app_db"
# Create the database and the database table
db.create_all(bind_key)
db.session.add(new_user)
db.session.commit()
# yield unlike return doesn't stop when called.
yield test_username_db()
yield # other functions
db.drop_all(bind_key)
test_fixture.py
test_fixture.py(create_db)
create_db()
Here is the exact error for example 3.
python -m pytest
================================================================= test session starts ==================================================================
platform win32 -- Python 3.10.8, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\Users\nmyle\OneDrive\Desktop\flaskcodeusethis\flaskblog2
collected 0 items / 1 error
======================================================================== ERRORS ========================================================================
____________________________________________________________ ERROR collecting test session _____________________________________________________________
..\..\..\..\Anaconda3\envs\py\lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
???
<frozen importlib._bootstrap>:1027: in _find_and_load
???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:688: in _load_unlocked
???
..\..\..\..\Anaconda3\envs\py\lib\site-packages\_pytest\assertion\rewrite.py:168: in exec_module
exec(co, module.__dict__)
app\tests\conftest.py:5: in <module>
from tests.functions_use_db import app, db, UserTest , email_token_and_registration_confirmation_email
E ModuleNotFoundError: No module named 'tests'
=============================================================== short test summary info ================================================================
ERROR - ModuleNotFoundError: No module named 'tests'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Like stated earlier how do I fix this?