gpt4 book ai didi

unit-testing - 如何从多个维度构建 BDD 单元测试?

转载 作者:行者123 更新时间:2023-12-03 00:31:24 27 4
gpt4 key购买 nike

考虑以下测试输出:

A passing tests spec illustrating my situation `

总而言之,不支持 Word 文档,但支持 PDF。所以我们立即拒绝Word文档。但对于 PDF,还有很多步骤需要测试,因此我们会执行这些步骤。

问题是,假设我还想支持文本文件,它的工作流程与 PDF 完全相同。我正在测试的代码基本上如下所示:

function uploadDocument(type, document) {
if (type !== "application/pdf" && type !== "text/plain") {
throw new UnsupportedMediaTypeError();
}

// do all the steps involving temp documents, hashing, ownership, etc.
}

我的问题是:我该如何构建我的测试?我不想将“上传 PDF 时”下方的整个树复制为“上传文本文件时”。

我觉得我经常遇到这个问题。正如你所看到的,我已经做了一些重复(“并且删除临时文档成功”和“并且提交临时文档成功”下的条目是相同的)。

本质上,这是一个改变系统的多个维度并组合测试它们的问题。一定有人想到了如何构建这样的测试。

最佳答案

我似乎需要将其分成两个测试(可能更多,但这是一个不同的主题)

Given A document of type <doc_type> 
Then it should have an allowed status of <status>

Examples:
| doc_type | status |
| word | fail |
| text | accept |
| pdf | accept |

然后你的测试将简化为

...
when uploading a valid file type
...

测试愉快,卢埃林

关于unit-testing - 如何从多个维度构建 BDD 单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506754/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com