如何在Go 1.18中运行一个源文件中编写的多个模糊测试用例?

huangapple go评论81阅读模式
英文:

how to run multi fuzz test cases wirtten in one source file with go1.18?

问题

go 1.18已经在几天前发布了。从Go 1.18开始,它的标准工具链支持模糊测试。

但是当我尝试编写我的测试用例时,它不能在一个包(或一个文件)中运行多个测试用例。
代码:

package xxx
func FuzzReverse(f *testing.F) {
    testcases := []string{"Hello, world", " ", "!12345"}
    for _, tc := range testcases {
        f.Add(tc) // 使用f.Add提供种子语料库
    }
    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) // 使用f.Add提供种子语料库
    }
    f.Fuzz(func(t *testing.T, orig string) {
        Reverse(orig)
    })
}

然后我运行命令:

go test -fuzz .

或者

go test -fuzz=Fuzz

但结果是:

testing: will not fuzz, -fuzz matches more than one fuzz test: [FuzzReverse FuzzReverse2]

就像这样:
如何在Go 1.18中运行一个源文件中编写的多个模糊测试用例?

教程没有提到这一点,谢谢帮忙。(这是我在stackoverflow上的第一个问题,非常感谢!!!)

我尝试在一个源文件中编写多个模糊测试用例,然后运行命令:go test -fuzz . 期望它进行模糊测试,但是得到了一个错误:

testing: will not fuzz, -fuzz matches more than one fuzz test: [FuzzReverse FuzzReverse2]

英文:

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:
如何在Go 1.18中运行一个源文件中编写的多个模糊测试用例?

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]

答案1

得分: 1

好的,以下是翻译好的内容:

好的,我已经阅读了Go-fuzz模块的源代码,事实上它不支持每次执行多个测试用例。

代码位于:\Go\src\testing\fuzz.go

if len(matched) > 1 {
	fmt.Fprintf(os.Stderr, "testing: will not fuzz, -fuzz matches more than one fuzz test: %v\n", matched)
	return false
}

我希望将来能够支持多个测试用例的执行。

英文:

all right,I've read the source of Go-fuzz module,
it's a fact that it not support multi cases for each execution.

code in :\Go\src\testing\fuzz.go

if len(matched) > 1 {
	fmt.Fprintf(os.Stderr, "testing: will not fuzz, -fuzz matches more than one fuzz test: %v\n", matched)
	return false
}

I hope that multi cases execution could be supported in the future.

答案2

得分: 1

这是一个快速而简单的bash脚本,它将在当前文件夹中找到所有的模糊测试,并每个测试运行10秒钟:

#!/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 "在 $file 中对 $func 进行模糊测试"
		parentDir=$(dirname $file)
		go test $parentDir -run=$func -fuzz=$func -fuzztime=${fuzzTime}s
	done
done

要使用这个脚本,创建一个名为fuzzAll.sh的新文件,并将代码复制到其中,然后运行./fuzzAll.sh以每个测试运行10秒钟,或者传递一个不同的数字以运行不同持续时间的测试(例如,./fuzzAll.sh 30表示每个测试运行30秒钟)。

有关更多参考信息,可以参考现有的GitHub问题,该问题讨论允许多个模糊目标的功能,但尚未确定添加的时间。

英文:

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.

huangapple
  • 本文由 发表于 2022年3月23日 16:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/71584005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定