Disable tests when using AWS SAM Build

Posted on Jan 9, 2023

Before I start, disabling tests as part of your build pipeline is bad - I don’t recommend it. However, in this scenario, sam builds, especially when running sam sync --watch can take a bit of time, and the feedback loop (write some code, and quickly see it running) can be several minutes, rather than seconds (which you would normally see when running unit tests etc).

So, when running sam sync --watch you may want to disable running of unit/integration tests to make things faster. I’d recommend ensuring unit tests and integration tests are ran somewhere else in your build pipeline.

Full Credit to Alexander Sparkowsky for his post on this : Disable Gradle tests when building projects with SAM.

Disable tests by adding the following to your build.gradle

allprojects {
    tasks.withType(Test::class) {
        onlyIf {
            System.getProperty("software.amazon.aws.lambdabuilders.scratch-dir") == null
                    || System.getenv("GRADLE_SAM_EXECUTE_TEST") != null
        }
    }
}

This will then disable tests when you run:

sam build

or

sam sync --watch

To override this and run tests, simple run the following:

GRADLE_SAM_EXECUTE_TEST=1 sam build