go 1.18 has released serveral days ago.It supports fuzzing in its standard toolchain beginning in Go 1.18
but while i'm trying to write my cases , it can not run multi cases in one package(or one file?). code:
package xxx
func FuzzReverse(f *testing.F) {
testcases := []string{"Hello, world", " ", "!12345"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
Reverse(orig)
})
}
func FuzzReverse2(f *testing.F) {
testcases := []string{"Hello, world", " ", "!12345"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
Reverse(orig)
})
}
and i run cmd:
go test -fuzz .
or
go test -fuzz=Fuzz
but the result is:
testing: will not fuzz, -fuzz matches more than one fuzz test: [FuzzReverse FuzzReverse2]
like this:
the tutorial didn't tips about it, thx for help.(my first question in stackoverflow, thx a lot!!!!)
I try to wirte multi fuzz cases in one source file,then run cmd: go test -fuzz . expecting it work fuzz-testing,but got an error:\
testing: will not fuzz, -fuzz matches more than one fuzz test: [FuzzReverse FuzzReverse2]
Here is a quick-n-dirty bash script which will find all the fuzz tests in the current folder and run them for 10 seconds each:
#!/bin/bash
set -e
fuzzTime=${1:-10}
files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .)
for file in ${files}
do
funcs=$(grep -oP 'func \K(Fuzz\w*)' $file)
for func in ${funcs}
do
echo "Fuzzing $func in $file"
parentDir=$(dirname $file)
go test $parentDir -run=$func -fuzz=$func -fuzztime=${fuzzTime}s
done
done
To use this script, create a new file named fuzzAll.sh
and copy this code into it, then run ./fuzzAll.sh
to run all fuzz tests for 10 seconds each, or pass a different number to run for a different duration (e.g. ./fuzzAll.sh 30
to run for 30 seconds each)
For further reference, there is an existing github issue for allowing multiple fuzz targets, but no ETA on when it will be added.
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