- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 DUnit 来测试 Delphi 库。我有时会遇到这样的情况,我编写几个非常相似的测试来检查函数的多个输入。
有没有办法在 DUnit 中编写(类似于)参数化测试?例如,为合适的测试过程指定输入和预期输出,然后运行测试套件并获取关于多次运行测试失败的反馈?
(编辑:示例)
例如,假设我有两个这样的测试:
procedure TestMyCode_WithInput2_Returns4();
var
Sut: TMyClass;
Result: Integer;
begin
// Arrange:
Sut := TMyClass.Create;
// Act:
Result := sut.DoStuff(2);
// Assert
CheckEquals(4, Result);
end;
procedure TestMyCode_WithInput3_Returns9();
var
Sut: TMyClass;
Result: Integer;
begin
// Arrange:
Sut := TMyClass.Create;
// Act:
Result := sut.DoStuff(3);
// Assert
CheckEquals(9, Result);
end;
我可能有更多这样的测试,它们做完全相同的事情,但具有不同的输入和期望。我不想将它们合并到一个测试中,因为我希望它们能够独立地通过或失败。
最佳答案
您可以使用 DSharp 来改进您的 DUnit 测试。尤其是新单位DSharp.Testing.DUnit.pas (在 Delphi 2010 及更高版本中)。
只需在 TestFramework 之后将其添加到您的用途中,您就可以向您的测试用例添加属性。那么它可能看起来像这样:
unit MyClassTests;
interface
uses
MyClass,
TestFramework,
DSharp.Testing.DUnit;
type
TMyClassTest = class(TTestCase)
private
FSut: TMyClass;
protected
procedure SetUp; override;
procedure TearDown; override;
published
[TestCase('2;4')]
[TestCase('3;9')]
procedure TestDoStuff(Input, Output: Integer);
end;
implementation
procedure TMyClassTest.SetUp;
begin
inherited;
FSut := TMyClass.Create;
end;
procedure TMyClassTest.TearDown;
begin
inherited;
FSut.Free;
end;
procedure TMyClassTest.TestDoStuff(Input, Output: Integer);
begin
CheckEquals(Output, FSut.DoStuff(Input));
end;
initialization
RegisterTest(TMyClassTest.Suite);
end.
当你运行它时,你的测试看起来像这样:
由于 Delphi 中的属性只接受常量,因此属性只将参数作为字符串,其中值之间用分号分隔。但是没有什么可以阻止您创建自己的属性类,这些属性类采用正确类型的多个参数来防止“魔术”字符串。无论如何,您只能使用 const 类型。
您还可以在方法的每个参数上指定 Values 属性,并以任何可能的组合调用它(如 NUnit )。
就我个人而言,我希望在编写单元测试时编写尽可能少的代码。另外,我想看看当我查看接口(interface)部分而不深入研究实现部分时测试会做什么(我不会说:“让我们做BDD”)。这就是为什么我更喜欢声明式方式。
关于delphi - 我可以在 DUnit 中编写 'parameterized' 测试吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999945/
我偶然发现了这个错误,但找不到问题所在: The method compute(Set, Set) in the type JaccardSimilarity is not applicable fo
我有一些测试,我想使用一些需要在收集阶段进行参数化的参数和一些需要在设置时进行参数化的参数进行参数化。我无法使用metafunc.parametrize 在 pytest_generate_test
我有一个 AbstractCacheBlock,我想定义一个 CacheSet 类,其中包含许多扩展 AbstractCacheBlock 的类。我相信我想要的是这样的 public class Ca
我想使用 Jenkins 中的参数化调度程序插件来使用 cron 运行多个因参数不同而不同的作业。可悲的是,文档很糟糕。谁能指导我如何真正让它发挥作用? 最佳答案 这个插件的作用: parameter
我遇到了这个错误 Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array,
我想创建一个方法来接受字符串数组类型的“参数化”输入对象。像这样的东西: public void MyMethod(params string[][] input) { //...do stuf
我正在尝试使用Criterion框架编写一个基于一些字符串值的参数化测试。作为一个MRE,并遵循文档中显示的示例,我写了以下内容:。但是,当我运行测试时,它崩溃了,输出如下:。但是,当我尝试基于一组i
我有一个 OCaml 程序,其中模块有很多依赖于参数的功能,即“维度”。此参数在代码运行开始时确定一次,并保持不变直到终止。 我的问题是:我怎样才能把代码写得更短,这样我的函数就不需要一个“维度”参数
我正在尝试创建一个实现黑莓 Comparator 的类这样我就可以轻松地对二维数组进行排序。 import net.rim.device.api.util.Comparator; class A
这就是我的困境。我一直在使用 Selenium、TestNG 和 iText 从自动化测试运行的结果中生成漂亮的 PDF 报告,但最近有人告诉我,他们不想要 TestNG 与 JUnit 的竞争库,并
我正在使用 Katalon Studio 测试 Web 应用程序,测试步骤包括通过文件选择器将文件加载到应用程序中。我正在使用 WebUI.uploadFile() 来执行此操作,但我发现它需要仅一个
这是我简单的 JUnit 参数化测试,我用 testdata 解析 csv,并尝试将其提供给 Parametrized.Parameters,但是当我运行它时,我的测试因 java.lang.Ille
我有一个应用程序页面,其中有 52 个连续命名的文本框,用于输入和显示一年的每周需求数字。我的问题是有没有一种方法可以“参数化”控件(在本例中为文本框)名称,以便我可以遍历它们而不必列出所有 52 个
我是 Spring 的新手,对它的功能只了解了皮毛。 我有一种情况需要使用 @Scheduled 注释来设置重复任务。速率被指定为对象中的成员字段,该对象传递给封装表示任务的方法的类。 我使用了允许访
在 Jenkins 中,我使用 SSH 插件通过 ssh 在远程机器上运行 shell 命令。我已将我的 SSH 站点添加到全局配置中。 但是在我的作业配置的“使用 ssh 在远程主机上执行 shel
我是 py.test 的新手。到目前为止,我喜欢我所看到的并希望将其集成到我们的 CI 过程中。 目前我们在测试中使用了一种不同的参数化方案,我将简要解释一下: 我们不是对每个测试进行参数化,而是对每
是否可以使用 & 符号编写参数化函数? 这是 Dave Thomas 的 Programming Elixir 书中的参数化函数示例 title = fn (title) -> ( fn (name)
我们在项目内部运行 Hudson 构建服务器。每次服务器部署新版本时,我们都会发送电子邮件。 我希望这封电子邮件包含更多信息,包括用户名和构建原因。 来自 Parameterized builds 上
这是一个困扰我一段时间的问题,我想知道这里是否有人可以提供帮助。 我有一个名为 lambdaLVar 的语言的 PLT Redex 模型,它或多或少是一种花园式的无类型 lambda 演算,但扩展了一
我已经测试在某些可执行文件上运行子进程并测试标准输出结果。 所以我用 #conftest.py def pytest_addoption(parser): parser.addoption("
我是一名优秀的程序员,十分优秀!