I am new to pytest and testing in general. I am running into issues with getting the data to the route. The content is empty.
pytest
conftest.py
@pytest.fixture()
def app():
app = create_app()
app.config['WTF_CSRF_ENABLED'] = False
yield app
@pytest.fixture()
def client(app):
return app.test_client()
test_project.py
def test_registration(client,app):
headers = {"Content-Type":"application/json"}
registerResponse = client.get("/register/")
html = registerResponse.get_data(as_text=True)
csrf_token = parse_form(html)
createResponseRaw = client.post("/create/", data = {"fullName":"George Washington","displayName":"gwashington","csrf_token":csrf_token},headers = {"Content-Type":"application/json"})
createResponse = return_response(createResponseRaw)
print(createResponse)
The post occurs to the route
@appBP.route('/create/',methods=['POST'])
def create():
msg = {"results":""}
print(request.method) #POST
print(request.form) #ImmutableMultiDict([])
print(request.headers.get('Content-Type')) #application/json
data = request.get_json()
The error that comes back is 400 Bad Request the browser or proxy sent a request that this server could not understand
When I print request.form or request.json
there is no data. I am sending the csrf_token, however I also disabled it so it is not needed.
I am new to pytest and testing in general. I am running into issues with getting the data to the route. The content is empty.
pytest
conftest.py
@pytest.fixture()
def app():
app = create_app()
app.config['WTF_CSRF_ENABLED'] = False
yield app
@pytest.fixture()
def client(app):
return app.test_client()
test_project.py
def test_registration(client,app):
headers = {"Content-Type":"application/json"}
registerResponse = client.get("/register/")
html = registerResponse.get_data(as_text=True)
csrf_token = parse_form(html)
createResponseRaw = client.post("/create/", data = {"fullName":"George Washington","displayName":"gwashington","csrf_token":csrf_token},headers = {"Content-Type":"application/json"})
createResponse = return_response(createResponseRaw)
print(createResponse)
The post occurs to the route
@appBP.route('/create/',methods=['POST'])
def create():
msg = {"results":""}
print(request.method) #POST
print(request.form) #ImmutableMultiDict([])
print(request.headers.get('Content-Type')) #application/json
data = request.get_json()
The error that comes back is 400 Bad Request the browser or proxy sent a request that this server could not understand
When I print request.form or request.json
there is no data. I am sending the csrf_token, however I also disabled it so it is not needed.
1 Answer
Reset to default 0It appears that caching is a big problem in pytest. I needed to run the command with --cache-clear
to fix my issues.
pytest test_project.py -v -s --cache-clear