4

i am using Flask to build a web service and pytest for testing

i am using pytest fixtures to set up and tear down the test resources but i need to test a POST endpoint that will create some records in the database

How do we clean up these records ?

  • Don't know about flask but if you are interested, use [restfulpy][https://github.com/Carrene/restfulpy] framework instead, it is easy to use and also super easy to write tests. If you have questions about it, I will be happy to help you. – iRhonin Dec 05 '18 at 08:49

1 Answers1

7

You can use a fixture to do that cleanup.

@pytest.fixture
def cleanup():
    yield
    # This is executed when the test using the fixture is done
    db_cleanup()

def test_records_created(cleanup):     # pylint: disable=redefined-outer-name,unused-argument
    response = app.test_client().post('/path', json=payload)
    assert response.status_code == 200
    assert ...
xverges
  • 4,608
  • 1
  • 39
  • 60