0

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?

trewbth
  • 13
  • 5

1 Answers1

0

Focusing on example 3, it may have something to do w/ the way Pytest loads the modules. When I reference my test module in my conftest.py file, I ran into errors. I would suggest writing the code to instantiate your database in another module other than the test module (maybe in your contest.py module). Also, I put all of my fixtures in my conftest module. So your code would look similar to this.

test_functions.py

#pass your fixture name as a parameter
def test_username_db(create_db):
    #reference your fixture in this function to call it 
    db_usr_name = create_db("Bob")
    assert db_usr_name != 'Bob'

conftest.py

import pytest

@pytest.fixture() 
def create_db():
    def _subfunc(new_user):
        
        #code to create your db here
        
        return db.user_name 
        
    yield 
    #do your tear down functions here (don't reference any functions in your test_functions.py module)

On fixtures, I use sub functions for parameters because it seems you can't use parameter like normal functions on fixtures (see here).

Sidenote: Your test will fail if the database correctly adds "Bob" as a new user. I don't know if that's what you intended.

jwill
  • 84
  • 7
  • I added some code to clarify my original question. The error was caused by `from tests.test_function import test_username_db` which should be `from .test_function import test_username_db`. Why was this causing the error? Mycode starts at `flaskblog2` and all the pytest code is in `flaskblog2\app\tests path`. I am also getting a second minor error but I will make a new post about that. – trewbth Apr 08 '23 at 02:20
  • I think you are getting the error because of the way Pytest compiles modules. When I tried to do something similar, Pytest loads Conftest.py into memory BEFORE your test module. So it doesn't "see" your test module when you run it. This would make sense because Conftest.py is used to set up the test environment before you test. I would suggest writing separate code to instantiate your database in your Conftest.py file. – jwill Apr 10 '23 at 11:56
  • You are correct your answer is also brilliant. Though like stated you need `from .test_function import test_username_db` Do you mind if edit your answer to be more detailed? – trewbth Apr 13 '23 at 05:43