Search code examples
pythontornado

Tornado: Reset database before running all tests


I am using Python Tornado web server. When I write test, before all tests, I want to do something (such as prepare some data, reset database ...). How can I achieve this in Python or Tornado web server.

In some languages I can easily do this. Example: in Golang, there is a file named main_test.go.

Thanks


Solution

  • In your test folder, you create __init__.py and initialize everything here.

    // __init__.py
    reset_database()
    run_migration()
    seed_data()
    

    Noted that, you should configure your project running test from root folder. For example, if your test inside app/tests/api/sample_api.py, your test should be run from app. In this case, __init__.py will always run before running your sample_api.py. Here is the command line I usually run for running all tests inside project:

    python -m unittest discover