I believe that this error occurs because of the function pandas.testing.assert_frame_equal returns None if frames are equal; if they're unequal it raises AssertionError. Therefore you're actually checking assert None.
So I think you should remove assert operator here and just write
Copytesting.assert_frame_equal(expected, result)
Or write
Copyassert testing.assert_frame_equal(expected, result) is None
Answer from Viacheslav Zhukov on Stack OverflowWarning about assert None from XFAIL tests
I have some code in pytest. I am trying to assert the code but none of the asserts are showing and no errors are showing.
python - Is there a pytest method similar to in assertIsNone(x) of unittest - Stack Overflow
PytestAssertRewriteWarning: asserting the value None, please use "assert is None"
I am using flask for the code.
I am also not getting any errors or any asserts.
I tried python -m pytest and pytest -q --capture=no. Everything else is working in the files. Why is this not working?
I didn't include the username_form etc but they are just fixtures that return the username etc.
Due to formating issues here is the code
https://paste.pythondiscord.com/obaqahuhok.py
Here is the code.
@pytest.fixture() def yield_selected_columns_PaymentsTest():''' in theCreate the UserTest and the PaymentsTest db. Then yield the PaymentsTest table and finally delete the db. yield does not stop the code when yielded. '''
with app.test_request_context(): # = with app.app_context() except won't work for pytest
bind_key="testing_app_db"
def _subfunction(username_form, hashed_password_form, email_form, item_name_form, price_of_donation_form):
# Create the databases and the database table
db.create_all(bind_key)
usertest_db = UserTest(username=username_form, hashed_password=hashed_password_form, email=email_form)
db.session.add(usertest_db)
db.session.commit()
payment_db = PaymentsTest(item_name=item_name_form, price_of_donation=price_of_donation_form)
db.session.add(payment_db)
db.session.commit()
current_email_form = os.environ['TESTING_EMAIL_USERNAME']
return current_email_form
# yield unlike return doesn't stop when called.
yield _subfunction
db.drop_all(bind_key) test_functions.py
def test_asserts(yield_selected_columns_PaymentsTest, username_form, hashed_password_form, email_form, item_name_form, price_of_donation_form):
'''
This runs in the /donation route.
If the email have the same value add the foreign key in the payment table.
You will always have a registered account when adding Foreign key.
I want the foregin key to be added from the db.
'''
assert 1 == 2
email_form = yield_selected_columns_PaymentsTest(username_form, hashed_password_form,
email_form, item_name_form, price_of_donation_form)
# if a email exists in the payment table
payment_db = PaymentsTest.query.filter_by(email=email_form).first()
assert payment_db != None
assert payment_db == None
# if email exists in the User table
user_db = UserTest.query.filter_by(email=email_form).first()
assert user_db == None
assert user_db != None
I based the code on this https://stackoverflow.com/questions/75924656/how-do-i-import-functions-and-classes-into-conftest-py-or-how-do-i-import-fixtu
Thanks
» pip install pytest-assert-utils
The code starts by running the fixture yield_user_db.
Inside the fixture there is a function. In the function it creates the db then adds some of the columns from the db into the db. Next the code selects a record from the db and returns user_db. Then the sub-function is yielded.
Yield is like a return statement except it doesn't stop the function.
Then the db is deleted.
I realize there is a better way to do this but can I just use 1 fixture yield_user_db to test create_token and verify_token?
Here is an alternative way https://www.youtube.com/watch?v=RLKW7ZMJOf4
app/tests/models.py
from flask_login import UserMixin
from time import time
import jwt
from app import db
class UserTest(UserMixin, db.Model):
__tablename__ = 'user_test'
__bind_key__ = "testing_app_db"
id = db.Column(db.Integer, primary_key=True)
# unique blocks the same usernames
# I can't have Nullable=False because it will make me add the columns everytime I add a column in User table
username = db.Column(db.String(80), unique=True)
hashed_password = db.Column(db.String(128))
email = db.Column(db.String(120), unique=True)
registration_confirmation_email = db.Column(db.Boolean, default=False)
# need a better backref name.
rel_payments = db.relationship('PaymentsTest', backref='profileinfo', lazy=True)
bind_key = "testing_app_db"
def create_token(self, expires_in=600):
SECRET_KEY = 'temp_secret_key'
# This Creates the randomly assigned token for 30 min
return jwt.encode({'create_token': self.id, 'exp': time() + expires_in}, SECRET_KEY, algorithm='HS256')
# use @staticmethod so I don't have to use the self variable.
@staticmethod
def verify_token(token): # token is equal to create_token after called.
SECRET_KEY = 'temp_secret_key'
try:
# jwt.decode() gets the User's id by running the code below
users_id = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])['reset_password']
except:
print('This is an invalid or expired token')
return None
# gives the current user.
user_db = db.session.execute(db.select(UserTest).filter_by(id=users_id)).scalar_one_or_none()
user_db = user_db
return user_db
def __repr__(self):
return f"<UserTest('{self.email}')>"
class PaymentsTest(db.Model):
'''
One to many relationship
This is the Many relationship.
'''
__bind_key__ = "testing_app_db"
__tablename__ = 'payments_test'
id = db.Column(db.Integer, primary_key=True)
item_name = db.Column(db.String(80)) # what value should this be
price_of_donation = db.Column(db.Integer)
# How do I turn email into the foreign key? todo.
email = db.Column(db.String(120))
fk_user_id = db.Column(db.Integer, db.ForeignKey('user_test.id'))
bind_key = "testing_app_db"
# what does this do?
def __repr__(self):
return f"<PaymentsTest('{self.email}')>" tests/functions_and_routes_testing/conftest.py
@pytest.fixture
def username_form():
username = 'fkpr[kfkuh'
return username
from argon2 import PasswordHasher
@pytest.fixture
def hashed_password_form():
plaintext_password_form = 'pojkp[kjpj[pj'
ph = PasswordHasher()
hashed_password_form = ph.hash(plaintext_password_form)
return hashed_password_form
import os
@pytest.fixture
def email_form():
email_form = os.environ['TESTING_EMAIL_USERNAME']
return email_form
@pytest.fixture
def yield_user_db():
'''
add the 1 table UserTest
'''
# with app.test_request_context(): # = with app.app_context() except won't work for pytest
with app.test_request_context():
bind_key="testing_app_db"
def _subfunction(username_form, hashed_password_form, email_form):
# Create the databases and the database table
db.create_all(bind_key)
usertest_db = UserTest(username=username_form, hashed_password=hashed_password_form, email=email_form)
db.session.add(usertest_db)
db.session.commit()
user_db = db.session.execute(db.select(UserTest).filter_by(email=email_form)).scalar_one_or_none()
return user_db
# yield unlike return doesn't stop when called.
yield _subfunction
db.drop_all(bind_key) tests\functions_and_routes_testing.py
from app.tests.models import UserTest
def test_token(yield_user_db, username_form, hashed_password_form, email_form):
user_db = yield_user_db(username_form, hashed_password_form, email_form)
token = user_db.create_token()
assert token != None
user_db = UserTest.verify_token(token)
assert user_db != None
» pip install pytest-check