I'm settling a friendly debate with my friend, Alex. I was wrong and he was right.
You're telling me you can execute 20 parametrized test HTTP calls to a Flask service with full database setup/teardown in under a second?
Yes.
Show me.
I doubted, but no more. It is definitely possible to execute those Flask tests in under a second (as long as you don't count container startup time).
This is obviously a toy application and depending on the scope of the pytest
fixture it's not always under a second, but it's usually within an acceptable
threshold. If you set the fixture scope to module
it's basically always
under a second, though that does risk considerable test impurity.
First, start the DB and build the test container.
docker-compose up -d db && docker-compose build test
Then, run the tests with function
scope (the default).
time SCOPE=function docker-compose up --no-log-prefix test
Recreating fast-tests_test_1 ... done
Attaching to fast-tests_test_1
============================= test session starts ==============================
platform linux -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /test
collected 20 items
app.py .................... [100%]
============================== 20 passed in 0.90s ==============================
fast-tests_test_1 exited with code 0
SCOPE=function docker-compose up --no-log-prefix test 0.40s user 0.05s system 19% cpu 2.299 total
Finally, run the tests again with module
scope just to see the improvement.
time SCOPE=module docker-compose up --no-log-prefix test
Recreating fast-tests_test_1 ... done
Attaching to fast-tests_test_1
============================= test session starts ==============================
platform linux -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /test
collected 20 items
app.py .................... [100%]
============================== 20 passed in 0.46s ==============================
fast-tests_test_1 exited with code 0
SCOPE=module docker-compose up --no-log-prefix test 0.42s user 0.05s system 24% cpu 1.974 total