|
| 1 | +# Testing |
| 2 | + |
| 3 | +This document contains the Moby code testing guidelines. It should answer any |
| 4 | +questions you may have as an aspiring Moby contributor. |
| 5 | + |
| 6 | +## Test suites |
| 7 | + |
| 8 | +Moby has two test suites (and one legacy test suite): |
| 9 | + |
| 10 | +* Unit tests - use standard `go test` and |
| 11 | + [testify](https://github.com/stretchr/testify) assertions. They are located in |
| 12 | + the package they test. Unit tests should be fast and test only their own |
| 13 | + package. |
| 14 | +* API integration tests - use standard `go test` and |
| 15 | + [testify](https://github.com/stretchr/testify) assertions. They are located in |
| 16 | + `./integration/<component>` directories, where `component` is: container, |
| 17 | + image, volume, etc. These tests perform HTTP requests to an API endpoint and |
| 18 | + check the HTTP response and daemon state after the call. |
| 19 | + |
| 20 | +The legacy test suite `integration-cli/` is deprecated. No new tests will be |
| 21 | +added to this suite. Any tests in this suite which require updates should be |
| 22 | +ported to either the unit test suite or the new API integration test suite. |
| 23 | + |
| 24 | +## Writing new tests |
| 25 | + |
| 26 | +Most code changes will fall into one of the following categories. |
| 27 | + |
| 28 | +### Writing tests for new features |
| 29 | + |
| 30 | +New code should be covered by unit tests. If the code is difficult to test with |
| 31 | +a unit tests then that is a good sign that it should be refactored to make it |
| 32 | +easier to reuse and maintain. Consider accepting unexported interfaces instead |
| 33 | +of structs so that fakes can be provided for dependencies. |
| 34 | + |
| 35 | +If the new feature includes a completely new API endpoint then a new API |
| 36 | +integration test should be added to cover the success case of that endpoint. |
| 37 | + |
| 38 | +If the new feature does not include a completely new API endpoint consider |
| 39 | +adding the new API fields to the existing test for that endpoint. A new |
| 40 | +integration test should **not** be added for every new API field or API error |
| 41 | +case. Error cases should be handled by unit tests. |
| 42 | + |
| 43 | +### Writing tests for bug fixes |
| 44 | + |
| 45 | +Bugs fixes should include a unit test case which exercises the bug. |
| 46 | + |
| 47 | +A bug fix may also include new assertions in an existing integration tests for the |
| 48 | +API endpoint. |
| 49 | + |
| 50 | +## Running tests |
| 51 | + |
| 52 | +To run the unit test suite: |
| 53 | + |
| 54 | +``` |
| 55 | +make test-unit |
| 56 | +``` |
| 57 | + |
| 58 | +or `hack/test/unit` from inside a `BINDDIR=. make shell` container or properly |
| 59 | +configured environment. |
| 60 | + |
| 61 | +The following environment variables may be used to run a subset of tests: |
| 62 | + |
| 63 | +* `TESTDIRS` - paths to directories to be tested, defaults to `./...` |
| 64 | +* `TESTFLAGS` - flags passed to `go test`, to run tests which match a pattern |
| 65 | + use `TESTFLAGS="-test.run TestNameOrPrefix"` |
| 66 | + |
| 67 | +To run the integration test suite: |
| 68 | + |
| 69 | +``` |
| 70 | +make test-integration |
| 71 | +``` |
0 commit comments