I'd like to have a BATS test created for every file in a directory, but I'm not sure what the best way to get this done is. The approach below only creates a single test even when there are many files in the directory.
#!/usr/bin/env bats
for i in ./*;do
@test "testing $i" {
pwd
}
done
When a test is run by BATS, the file is first preprocessed.1
This replaces the @test block with an actual function and adds a call to
that function.
The result is then stored in the BATS_TMPDIR as bats.${PID}.src.
Any programmatically added tests would need to be added to the preprocessed file.
The test names would also have to be added to BATS_TEST_NAMES.
Putting all of this together we get:2
#!/usr/bin/env bats
declare sFile sSourceFile sTestFunction sTestName
readonly sSourceFile="${BATS_TMPDIR}/bats.$$.src"
if [[ -f "${sSourceFile}" ]];then
for sFile in ./*;do
sTestFunction="test_${sFile}"
sTestName="Testing ${sFile}"
cat <<EOT >> "${sSourceFile}"
$sTestFunction() { bats_test_begin '$sTestName' 0;
return 0
}
bats_test_function "${sTestFunction}"
EOT
BATS_TEST_NAMES+=("${sTestFunction}")
done
fi
#EOF
The preprocessed version of your example looks like this:
#!/usr/bin/env bats
for i in ./*; do
test_testing_-24i() { bats_test_begin "testing $i" 4;
pwd
}
done
bats_test_function test_testing_-24i
So effectively, the test function is declared as many times as there are files present. The test function, however, is only called once.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With