I'm getting started with Pytest and I'm trying to have two test functions hit the same endpoint. The first test can pass or fail, but the second test always fails with a 404 Not Found error. The two test functions are now identical, whichever runs second fails. Not really finding anything on why this is. I've even tried just swapping the functions to see what would happen (the second function always fails).
Most of the suggestions online are solved by adding a conftest.py, which I've already done.
The code:
conftest.py
import pytest
from flaskr import create_app
@pytest.fixture()
def app():
app = create_app(True)
app.config.update({
"TESTING": True,
})
yield app
@pytest.fixture()
def client(app):
return app.test_client()
@pytest.fixture()
def runner(app):
return app.test_cli_runner()
The tests:
import pytest
import json
def test_register_fail(client):
headers = {
'Content-Type': 'application/json',
'Accepts': 'application/json'
}
response = client.post("/register", data=json.dumps({
"name": "Test User",
"email_address": "alex@alex.alex@",
"password": "test123"
}), headers=headers)
print(response.data)
assert response.status_code == 200
def test_register(client):
headers = {
'Content-Type': 'application/json',
'Accepts': 'application/json'
}
response = client.post("/register", data=json.dumps({
"name": "Test User",
"email_address": "alex@alex.alex@",
"password": "test123"
}), headers=headers)
print(response.data)
assert response.status_code == 200
I could understand if the second test was failing on a unique constraint error or something related to my code, but it appears it's not even hitting the endpoint. The error:
b'<!doctype html>\n<html lang=en>\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>\n'
Any ideas?
edit After more testing and playing around, I've found some things that might be clues?
-
If I use
scope="module", it seems to work only if I run a single file at a time (iepytest tests/test_registerwill successfully run both tests in test_register successfully) -
If I use
scope="module"and try to run all test files (ie just runningpytest), only the first file loaded will run, the second will fail. By creatingtest_login.py, I've made a file that is run beforetest_register, so test_login runs correctly and test_register always fails with a 404
from Only a single Flask test can run, the second test always fails with "Not Found" even if the content is the same
No comments:
Post a Comment