github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/TESTING.md (about)

     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    [gotest.tools/assert](https://godoc.org/gotest.tools/assert) 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    [gotest.tools/assert](https://godoc.org/gotest.tools/assert) 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  ### Integration tests environment considerations
    51  
    52  When adding new tests or modifying existing test under `integration/`, testing 
    53  environment should be properly considered. `skip.If` from 
    54  [gotest.tools/skip](https://godoc.org/gotest.tools/skip) can be used to make the 
    55  test run conditionally. Full testing environment conditions can be found at 
    56  [environment.go](https://github.com/moby/moby/blob/cb37987ee11655ed6bbef663d245e55922354c68/internal/test/environment/environment.go)
    57  
    58  Here is a quick example. If the test needs to interact with a docker daemon on 
    59  the same host, the following condition should be checked within the test code
    60  
    61  ```go
    62  skip.If(t, testEnv.IsRemoteDaemon())
    63  // your integration test code
    64  ```
    65  
    66  If a remote daemon is detected, the test will be skipped.
    67  
    68  ## Running tests
    69  
    70  To run the unit test suite:
    71  
    72  ```
    73  make test-unit
    74  ```
    75  
    76  or `hack/test/unit` from inside a `BINDDIR=. make shell` container or properly
    77  configured environment.
    78  
    79  The following environment variables may be used to run a subset of tests:
    80  
    81  * `TESTDIRS` - paths to directories to be tested, defaults to `./...`
    82  * `TESTFLAGS` - flags passed to `go test`, to run tests which match a pattern
    83    use `TESTFLAGS="-test.run TestNameOrPrefix"`
    84  
    85  To run the integration test suite:
    86  
    87  ```
    88  make test-integration
    89  ```