Python Forum

Full Version: I am trying to run pytest and getting error that doesn't show up when running pytest.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

This is in python flask.

I am getting an error in pytest. When I run pytest it says it is working but I get the output below
Output:
app\tests\functional\test_routes.py . [ 50%] app\tests\unit\test_models.py . [100%]
My code is very similar to this https://gitlab.com/patkennedy79/flask_us...nt_example . When I run pytest -m pytest The code says it works but I get an error. When I put my cursor over



def init_database(test_client): 
,
test client
The error I get is https://imgur.com/a/lDHIPb4


Output:
(parameter) make_app_run_in_test_env: Any "make_app_run_in_test_env" is not accessed Pylance No quick fixes available




I named
test_client 
instead to
make_app_run_in_test_env.
Also it says pytest is working., but I know it can't be completely working because I added a line from the file https://gitlab.com/patkennedy79/flask_us...t_users.py

def test_invalid_login(test_client, init_database):
    # code not included.
    # The added line is below
    assert b'zzzzzzzzzzzzzzzz' in response.data 
and an error doesn't show up. I do not have zzzzzzzzzzzzzzzz anywhere in the html . Any help is appreciated. Also I am using a similar setup to this to run the code.


https://github.com/CoreyMSchafer/code_sn...Flask_Blog

Also I have 2 tests 1 tests works and the other doesn't, which I mentioned above ,in
Output:
app\tests\functional\test_routes.py . [ 50%] .
app\tests\functional\test_routes.py is the same as tests\functional\test\test_users.py in gitlab ,just with different file names and a few different variables.


Here is my exact code that is causing the error



app/tests/conftest.py


from app.models import User 
import bcrypt 
import pytest 
from app import create_app
from app import db


# function for tests_models.py
@pytest.fixture(scope='module')
def new_user():
    ''' 
    Given a User model
    When a new user is being created 
    Check the User database columns
    '''
    
    plaintext_password = 'jotpjgjbgt' 
    # username, hashed_password, email
    user = User('ugbuighiug' ,'[email protected]', plaintext_password)
    return user

# function for test_routes.py 
@pytest.fixture(scope='module')
def make_app_run_in_test_env():
    flask_app = create_app()
    # The with statemnt allows you to open and close files safely by making sure there is no errors
    # What is test_client?  test_client makes requests to the application without running a live server
    # Use the test_client to check the route which is a get request
    with flask_app.test_client() as testing_client: 
        # What is this line?
        with flask_app.app_context():
            yield testing_client




  
   
# function for test.routes.py
@pytest.fixture(scope='module')
def init_database(make_app_run_in_test_env):

    # Create the database and the database table 
    db.create_all()
    # Insert user data
    # fill out the data that is new.  
    user1 = User(username='grvfhtbynjuki', email='[email protected]', plaintext_password='ghbyunjikmol')
    db.session.add(user1)
    # Commit the changes for the users
    db.session.commit()
    yield  # this is where the testing happens!

    db.drop_all() # delete table after use

tests/functional/test_routes.py



def test_register_page_get(make_app_run_in_test_env):
    """ 
    GIVEN a Flask application configured for testing
    WHEN the '/register requested (GET) 
    THEN check that the response is valid 
    """
 
    
    response = make_app_run_in_test_env.get('/register')
    """
    In Python, the assert statement is used to continue the execute if the given condition evaluates to True. 
    If the assert condition evaluates to False, then it raises the AssertionError exception with the specified error message.
    """
    # Why use a b string? What is response.data? Answer it only work if I have a status code 200.
    assert response.status_code == 200
    assert b'register' in response.data



def valid_login_logout(make_app_run_in_test_env, init_database):
    """ 
    Given a flask app tests if it runs  
    When I check to make valid login and logout (POST) request
    Then I should be able to check login and logout using pytest
    """

    data = dict(username='ugbuighiug', email='[email protected]',  plaintext_password_='efrvgtbyhnu') 
    response = make_app_run_in_test_env.get('/login', data, follow_redirects=True)
    assert response.status_code == 200
    assert b'ttttttttttttttttt' in response.data
     


''' 
def test_register_page_post(test_client):
    """ 
    GIVEN a Flask application configured for testing
    WHEN the '/register'page requested (post) 
    THEN check that the response is valid 
    """

    response = test_client.post('/register')
    """
    In Python, the assert statement is used to continue the execute if the given condition evaluates to True. 
    If the assert condition evaluates to False, then it raises the AssertionError exception with the specified error message.
    """
    # Why use a b string? What is response.data? Answer it only work if I have a status code 405.
    assert response.status_code == 200
    # checking html for the term register
    assert b'register' in response.data
'''
Thanks.
I managed to fix the code simple error
def init_database(make_app_run_in_test_env):
should just be def init_database() .