github.com/GoogleContainerTools/skaffold/v2@v2.13.2/docs-v2/content/en/docs/testers/custom.md (about)

     1  ---
     2  title: "Custom Test"
     3  linkTitle: "Custom Test"
     4  weight: 20
     5  featureId: test.custom
     6  aliases: [/docs/pipeline-stages/testers/custom]
     7  ---
     8  
     9  
    10  Custom Test allows developers to run custom commands as part of their development pipeline. The command executes in the testing phase of the [Skaffold pipeline](https://skaffold.dev/docs/). It will run on the local machine where Skaffold is being executed and works with all supported Skaffold platforms. Users can opt out of running custom tests by using the `--skip-tests` flag.
    11  
    12  
    13  Some example use cases for Custom Test are below:
    14  - Run unit tests
    15  - Run validation and security scans on images before deploying the image to a cluster for example by running [GCP Container Analysis](https://cloud.google.com/container-analysis/docs/on-demand-scanning-howto) or [Anchore Grype](https://github.com/anchore/grype#readme)
    16  
    17  
    18  Custom tests are defined on a per image basis in the Skaffold config. Every time an artifact is rebuilt, Skaffold runs the associated custom tests as part of the Skaffold dev loop. Multiple testers can be defined per test. The Skaffold pipeline will be blocked on the custom test to complete or fail. Skaffold will block deployment when the first test fails. For ongoing test failures in the dev loop, Skaffold will stop the loop (not continue with the deploy) but will not exit the loop. Skaffold would surface the errors to the user and will keep the dev loop running. Skaffold will continue watching user specified test dependencies and re-trigger the loop whenever it detects another change. 
    19  
    20  CustomTester has a configurable timeout option to wait for the command to return. If no timeout is specified, Skaffold will wait indefinitely until the test command has completed execution.
    21  
    22  ### Contract between Skaffold and Custom command
    23  
    24  Skaffold will pass in the environment variable `$IMAGE` to the custom command to access the image.
    25  
    26  This variable can be set as a flag value input to the custom command `--flag=$IMAGE`.
    27  
    28  
    29  ### Configuration
    30  To use a custom command, add a custom field to the corresponding test in the test section of the skaffold.yaml. Supported schema for CustomTest includes:
    31  
    32  
    33  {{< schema root="CustomTest" >}}
    34  
    35  
    36  
    37  ### Dependencies for a Custom Test
    38  
    39  Users can specify `dependencies` for custom tests so that skaffold knows when to retest during a dev loop. Dependencies can be specified per command. Users could list out directories and/or files (for example test scripts)  to watch per command. If no dependencies are specified, only the script file (if the command is a script file) will be watched as a dependency. Test dependencies cannot trigger rebuild of an image.
    40  
    41  Supported schema for `dependencies` include:
    42  
    43  {{< schema root="CustomTestDependencies" >}}
    44  
    45  
    46  #### Paths and Ignore
    47  
    48  `Paths` and `Ignore` are arrays used to list dependencies. This can be a glob. Any `paths` in `Ignore` will be ignored by the skaffold file watcher, even if they are also specified in `Paths`. `Ignore` will only work in conjunction with `Paths`.
    49  
    50  ```yaml
    51      custom:
    52        - command: ./test.sh
    53          timeoutSeconds: 60
    54          dependencies:
    55            paths:
    56            -  "*_test.go"
    57            -  "test.sh"
    58  ```
    59  
    60  #### Command for dependencies
    61  
    62  Sometimes users might have a command or a script that can provide the dependencies for a given test. Custom tester can ask Skaffold to execute a custom command, which Skaffold can use to get the dependencies for the test for file watching.
    63  
    64  The command *must* return dependencies as a JSON array, otherwise skaffold will error out.
    65  
    66  ```yaml
    67      custom:
    68        - command: echo Hello world!!
    69          dependencies:
    70            command: echo [\"main_test.go\"] 
    71  ```
    72  
    73   
    74  >*Note: Adding a file pattern to a test dependency doesn't automatically enable file sync on it.  Refer to the [`file sync`](https://skaffold.dev/docs/filesync/) documentation, on how to set that up separately.*
    75  
    76  
    77  ### Logging
    78  
    79  `STDOUT` and `STDERR` from the custom command script will be redirected and displayed within skaffold logs.
    80  
    81  
    82  ## Usage
    83  
    84  Custom tests will be automatically invoked as part of the run and dev commands, but can also be run independently by using the test subcommand.
    85  
    86  - To execute the custom command as an independent test command run:
    87  ```skaffold test```
    88  - To execute custom command as part of the run command run:
    89  ```skaffold run```
    90  - To execute custom command as part of the dev loop run:
    91  ```skaffold dev```
    92  ### Example
    93  This following example shows the `customTest` section from a `skaffold.yaml`.
    94  It instructs Skaffold to run unit tests (main_test.go) located in the local folder when the main application changes:
    95  {{% readfile file="samples/testers/custom/customTest.yaml" %}}
    96  A sample `test.sh` file, which runs unit tests when the test changes.
    97  ```
    98  #!/bin/bash
    99  
   100  set -e
   101  
   102  echo "go custom test $@"
   103  
   104  go test .
   105  ```
   106  
   107  
   108