0
I’m new to python testing using Django and flask. Using the pytest library, how would test implementation of the following methods and what could be tested? If someone could give me a light it would help a lot!
import os
from flask import Flask, session, flash, redirect, render_template, request
from src.dao import UserDao
app = Flask(__name__)
secret = os.environ.get('SESSIONKEY')
app.secret_key = secret
user = UserDao()
@app.route('/logout')
def logout():
session['usuario_logado'] = None
session.modified = True
return redirect('/')
@app.route('/autenticar', methods=['POST'])
def autenticar():
if (user.read(request.form['email'], request.form['senha'])):
session['usuario_logado'] = user.nome
session.modified = True
return redirect('/perfil')
else:
flash("Email ou senha incorretos")
return redirect('/login')
@app.route('/criar', methods=['POST'])
def criar():
if (user.create(request.form['email'], request.form['nome'], request.form['senha'], request.form['valida_senha'])):
session['usuario_logado'] = request.form['nome']
return redirect('/perfil')
else:
flash(user.message)
return redirect('/register')
What I tried to do
def test_logout_page():
with app.test_client() as test_client:
response = test_client.get('/logout')
assert response.status_code == 200
def test_autenticar_page():
with app.test_client() as test_client:
response = test_client.get('/login')
assert response.status_code == 200
def test_criar_page():
with app.test_client() as test_client:
response = test_client.get('/register')
assert response.status_code == 200
Results
In test_logout_page() test it fails:
The other tests pass, but I found it very simple. What would be a way to create a more robust test, like creating a user and validating a request, ending a session on the page and so on?
I do not understand how the other tests are passing. In the test you make a
request.get
and in the view you specify the methodPOST
. Are you sure the code you posted is the one being tested? Something else : I believe thatapp.test_client
be a fixture, correct? Post her here too.– Paulo Marques
I only use the app.py file where it contains the endpoints and test_app.py test file that contains the endpoint tests, this one app.test_client I took from an example.
– Imai
Make a Troubleshooting of your test, leaving only the
test_autenticar_page
then comment here the result.– Paulo Marques
The test eventually passed: ============================== test Session Starts ============================= Platform linux -- Python 3.6.9, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /home/rseiji/Workspace-faculdade/tech.Amy/venv/bin/python cachedir: . pytest_cache rootdir: /home/rseiji/Workspace-estudo/tech.Academy/tests/Unit plugins: cov-2.10.1 Collecting ... collected 1 item test_app.py::test_autenticar_page PASSED [100%]

============================== 1 passed in 1.33s ===============================

Process finished with exit code 0
– Imai
Not seeing the fixture is difficult. Already tried to configure the POST method as in other views?
– Paulo Marques