英文:
Allow test result to depend upon installation of external tool
问题
我正在创建一个 Golang 项目,并且在添加功能时很好地进行了测试。总体思路是在文件和 git 分支之间执行“语义差异”检查。对于最新的功能,行为取决于是否安装了外部工具(tree-sitter-cli
)以及安装了哪些额外功能。
如果未安装外部工具(或其可选语法),我期望从我的内部函数中得到不同的结果。然而,这两个结果都与我的工具(sdt
)本身的正确性一致。例如,这是一个我从另一种编程语言中修改的测试,该语言在没有 tree-sitter 分析的情况下进行了分析:
func TestNoSemanticDiff(t *testing.T) {
// 将选项缩小到这些测试文件
opts := options
opts.Source = file0.name
opts.Destination = file1.name
report := treesitter.Diff("", opts, config)
if !strings.Contains(report, "| No semantic differences detected") {
t.Fatalf("Failed to recognize semantic equivalence of %s and %s",
opts.Source, opts.Destination)
}
}
对于各种支持的语言,测试的形式大致相同。然而,在这种情况下,如果外部工具缺失(或未包含该功能),接收到“report”的“| No available semantic analyzer for this format”也将是一个正确的值。
问题是,我可以运行代码来确定预期的值;但是最好的情况是,该代码应该是“在测试的顶部运行一次”,而不是在每个相似的测试中重新检查。
所以,我理想情况下想要的是首先运行某种设置/脚手架,然后编写类似以下的单独测试:
if !treeSitterInstalled { // 在测试文件开始时检查一次
if !strings.Contains(report, "appropriate response") { ... }
} else if treeSitterNoLangSupport { // 同样,在配置一次后
if !strings.Contains(report, "this other thing") { ... }
} else {
if !string.Contains(report, "the stuff where it's installed") { ... }
}
英文:
I am creating a Golang project, and doing a pretty good job of adding tests in step with the features. The general idea is that I perform a "semantic diff" between files and git branches. For the newest feature, the behavior depends upon whether an external tool (tree-sitter-cli
) is installed, and upon which extra capabilities are installed.
If the external tool (or its optional grammars) are not installed, I expect different results from my internal function. However, both results are consistent with my tool (sdt
) itself being correct. For example, here is a test I have modified from another programming language analyzed without tree-sitter:
func TestNoSemanticDiff(t *testing.T) {
// Narrow the options to these test files
opts := options
opts.Source = file0.name
opts.Destination = file1.name
report := treesitter.Diff("", opts, config)
if !strings.Contains(report, "| No semantic differences detected") {
t.Fatalf("Failed to recognize semantic equivalence of %s and %s",
opts.Source, opts.Destination)
}
}
The tests for various supported languages are mostly the same in form. In this case, however, receiving the "report" of "| No available semantic analyzer for this format" would also be a correct value if the external tool is missing (or did not include that capability).
The thing is, I can run code to determine which value is expected; but that code would ideally be "run once at top of test" rather than re-check within each of many similar tests.
So what I'd ideally want is to first run some sort of setup/scaffolding, then have individual tests written something like:
if !treeSitterInstalled { // checked once at start of test file
if !strings.Contains(report, "appropriate response") { ... }
} else if treeSitterNoLangSupport { // ditto, configured once
if !strings.Contains(report, "this other thing") { ... }
} else {
if !string.Contains(report, "the stuff where it's installed") { ... }
}
答案1
得分: 2
在你的测试文件中,你可以简单地使用init函数来检查外部依赖,并在同一个文件中设置一个变量,然后在各个测试中进行检查。
英文:
In your test file you could simply use the init function to do the checking for the external dependency and set a variable inside that very same file, which can then be checked inside individual tests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论