[FIXED] I am getting the error test_verified_email – TypeError: 'NoneType' object is not callable.This error happens the 1st time I run the code. Why is this?

Issue

I am getting the error test_verified_email – TypeError: ‘NoneType’ object is not callable. This error happens the 1st time I run the code. Why is this?

conftest.py

@pytest.fixture()
def new_user():

    
    # why can't I go plaintext_password() instead of plaintext_password 
    
    plaintext_password = 'pojkp[kjpj[pj'
    hashed_password = bcrypt.hashpw(plaintext_password.encode('utf-8'), bcrypt.gensalt())  
    current_user = User(username='fkpr[kfkuh', hashed_password=hashed_password, email=os.environ['TESTING_EMAIL_USERNAME'],
    confirmation_email=False, reset_email_password=False)
    return current_user

1st time running the code, the code works.

test_routes.py

@pytest.fixture()
def add_and_delete_database_try(new_user):            
    try:
        db.session.add(new_user)
        db.session.commit()      
        db.session.delete(new_user)
        db.session.commit()
    except:
        # what does rollback do vs delete and commit?
        db.session.rollback()



def test_verified_email(token_client, new_user, add_and_delete_database_try):   
    response = token_client.get("/verified_email<token>", follow_redirects=True)
    assert response.status_code == 200
    with token_app.test_request_context():
        add_and_delete_database_try(new_user)

2nd time running the code it runs

test_routes.py

def test_verified_email(token_client, new_user, add_and_delete_database_try):   
    response = token_client.get("/verified_email<token>", follow_redirects=True)
    assert response.status_code == 200
        with token_app.test_request_context():
            try:
                db.session.add(new_user)
                db.session.commit()      
                db.session.delete(new_user)
                db.session.commit()
            except:
                db.session.rollback()

Here is the error.
https://pastebin.com/EuFx5aNT

Solution

A @pytest.fixture() is not meant to be callable itself; it’s meant to be used in a test’s signature, and to return or yield an object (a fixture) to be used in the test.

Since you don’t return anything from that function, it’s probably not meant to be a fixture at all:

def add_and_delete_database_try(new_user):            
    try:
        db.session.add(new_user)
        db.session.commit()      
        db.session.delete(new_user)
        db.session.commit()
    except:
        # what does rollback do vs delete and commit?
        db.session.rollback()



def test_verified_email(token_client, new_user):   
    response = token_client.get("/verified_email<token>", follow_redirects=True)
    assert response.status_code == 200
    with token_app.test_request_context():
        add_and_delete_database_try(new_user)

Answered By – AKX

Answer Checked By – Marilyn (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published