Fixtures

Learn how to inject data into and perform activities before and after your tests.

What is a fixture?

Merriam-Webster’s dictionary defines a fixture as “a device for supporting work during machining.” This definition is pretty close to what we mean when we talk about fixtures in software testing. We can think of test fixtures as boundary conditions or preconditions that have to be verified for a test to begin. Many people think a test fixture is all the system setup you need for tests to run. That’s exactly what preconditions are—something that must be true at the beginning of a flow.

Classic fixture examples include database connections, sockets, and files. But they can also be simple objects or values that we use in our tests. If a test is based on an external value, that means the value must already be there, initialized as expected, for the test to run. We’ve been using fixtures in our code samples so far without even realizing it. Remember the EducativeSpec class? Let’s take a look at an extract.

Get hands-on with 1400+ tech skills courses.