So you like to write your tests in spec format (using describe,
before, after, and it) and running them with
minitest. You have a set of
tests with a common setup and that setup is orders of magnitude more
expensive than the body of each test, for example.
Running the example takes you 4 to 5 seconds because starting the HTTP
server is expensive. You would rather setup once before all tests and
teardown once after all tests. In the case where you are a practical
person, you use blocks before(:all) and after(:all) provided by gem
minitest-hooks like so.
Watch out. minitest-hooks executes a given before(:all) before EACH
nested describe block and a corresponding after(:all) after EACH
describe block. Consider the following spec file.
The spec file will give you an output like the following.
To execute your setup before all describe blocks and your teardown
after all describe blocks, you could do the following.
If you have aversion to global variables, you can do the following.
I am looking for another case
You know what, maybe you like writing specs or classes, maybe you have
a flat or nested describe block, maybe you want eager or lazy
evaluation, maybe you like using minitest-hooks or not. Why don’t
you have a look at 16 cases?
FYI, issue 61 of
minitest inspired me
to write this post and the 16 cases. You may want to have a look at
that for an idea of why minitest does not come with before(:all)/after(:all).
Want to read more?
I love to explain and answer questions on programming problems, the kind you find in coding interviews.
I publish a new programming problem and its solution every Sunday.
Did I mention that I love to answer questions?