This is a bats test script I wrote
export UUID=$(openssl rand -hex 6)
export TEST_NAME=SecretsManagerTest1-$UUID
setup_file() {
echo "test name is $TEST_NAME" > setup.txt
}
@test "addition using bc" {
echo "test name is $TEST_NAME" > runtest.txt
}
I want $TEST_NAME to have the same value in setup.txt and runtest.txt but they have different UUIDS(same prefix - SecretsManagerTest1)
How can I accomplish this?
When
bats
runs a test suite, it executes a file multiple times, once per test, since each test has its own shell, and once forsetup_file
.This in turn means, that the whole file will be
source
several times, executing the "free code" outside any functions each time.If you want to retain the same UUID for all tests in a
.bats
file, you should move the UUID generation intosetup_file
. Note thatsetup_file
variable that should be visible to the tests must beexport
ed, since the tests run in a subshell.