如何以编程方式按名称禁用 DUnit 测试?

发布于 2024-10-04 14:24:58 字数 176 浏览 4 评论 0原文

对于集成测试,我创建了一个 DUnit 测试套件,该套件为第三方组件(消息代理)的每个版本运行一次。不幸的是,由于被测试组件的某些版本中存在已知错误,某些测试总是失败。

这意味着测试套件永远不会 100% 完成。然而,对于自动化测试,需要 100% 的成功分数。 DUnit 不提供现成的方法来按名称禁用测试套件中的测试。

For integration tests, I created a DUnit test suite which runs once for every version of a third party component (a message broker). Unfortunately, some tests always fail because of known bugs in some versions of the tested component.

This means the test suites will never complete with 100%. For automated tests however, a 100% success score is required. DUnit does not offer a ready-made method to disable tests in a test suite by name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

违心° 2024-10-11 14:24:58

我编写了一个过程,它采用一个测试套件和一个测试名称列表,禁用具有匹配名称的所有测试,并且还执行嵌套测试套件的递归。

procedure DisableTests(const ATest: ITest; const AExclude: TStrings);
var
  I: Integer;
begin
  if AExclude.IndexOf(ATest.Name) <> -1  then
  begin
    ATest.Enabled := False;
  end;
  for I := 0 to ATest.Tests.Count - 1 do
  begin
    DisableTests(ATest.Tests[I] as ITest, AExclude);
  end
end;

用法示例(TStringlist“排除”是在设置方法中创建的):

procedure TSuiteVersion1beta2.SetUp;
begin
  // fill test suite
  inherited;

  // exclude some tests because they will fail anyway
  Excludes.Add('TestA');
  Excludes.Add('TestB');

  DisableTests(Self, Excludes);
end;

I wrote a procedure which takes a test suite and a list of test names, disables all tests with a matching name, and also performs a recursion into nested test suites.

procedure DisableTests(const ATest: ITest; const AExclude: TStrings);
var
  I: Integer;
begin
  if AExclude.IndexOf(ATest.Name) <> -1  then
  begin
    ATest.Enabled := False;
  end;
  for I := 0 to ATest.Tests.Count - 1 do
  begin
    DisableTests(ATest.Tests[I] as ITest, AExclude);
  end
end;

Example usage (the TStringlist ‘Excludes’ is created in the Setup method):

procedure TSuiteVersion1beta2.SetUp;
begin
  // fill test suite
  inherited;

  // exclude some tests because they will fail anyway
  Excludes.Add('TestA');
  Excludes.Add('TestB');

  DisableTests(Self, Excludes);
end;
简单爱 2024-10-11 14:24:58

对于已知会失败的测试,您可以实现 ITestListener 接口并覆盖 ShouldRunTest

此解决方案的优点(与 @WarrenP 的 DisableTests 相比)是,它不会影响确定要运行哪些测试的复选框。

我为我的测试套件使用通用基类 TMvTestSuite,它添加了一个可以在构造函数中调用的函数 AddKnownInvalidTest(例如在 AddTest 之后) :

type

    /// Listener that only implements ShouldRunTest
    TIShouldRunTestListener = class(TInterfacedObject, ITestListener)
    private
        FInvalidTests: TStrings;
        function IsInvalidTest(ATest: ITest): Boolean;
    protected
        function  ShouldRunTest(ATest: ITest):Boolean;
        procedure Status(test :ITest; const Msg :string);
        procedure TestingStarts;
        procedure StartTest(test: ITest);
        procedure AddSuccess(test: ITest);
        procedure AddError(error: TTestFailure);
        procedure AddFailure(Failure: TTestFailure);
        procedure EndTest(test: ITest);
        procedure TestingEnds(testResult :TTestResult);
    public
        constructor Create(AInvalidTests: TStrings);
    end;

    TMvTestSuite = class abstract(TTestSuite, ITestSuite, ITest)
    private
        FInvalidTests: TStrings;
    protected
        procedure RunTest(TestResult: TTestResult); override;
    protected
        procedure AddKnownInvalidTest(ATestName: string);
    public
        destructor Destroy; override;
    end;

implementation

constructor TIShouldRunTestListener.Create(AInvalidTests: TStrings);
begin
    FInvalidTests := AInvalidTests;
end;

function TIShouldRunTestListener.IsInvalidTest(ATest: ITest): Boolean;
begin
    Result := Assigned(FInvalidTests)
      and (FInvalidTests.IndexOf(ATest.Name) <> -1);
end;

function TIShouldRunTestListener.ShouldRunTest(ATest: ITest): Boolean;
begin
    Result := not IsInvalidTest(ATest);
end;

procedure TIShouldRunTestListener.AddError(error: TTestFailure);
begin
end;

procedure TIShouldRunTestListener.AddFailure(Failure: TTestFailure);
begin
end;

procedure TIShouldRunTestListener.AddSuccess(test: ITest);
begin
end;

procedure TIShouldRunTestListener.EndTest(test: ITest);
begin
end;

procedure TIShouldRunTestListener.StartTest(test: ITest);
begin
end;

procedure TIShouldRunTestListener.Status(test: ITest; const Msg: string);
begin
end;

procedure TIShouldRunTestListener.TestingEnds(testResult: TTestResult);
begin
end;

procedure TIShouldRunTestListener.TestingStarts;
begin
end;

{ TMvTestSuite }

destructor TMvTestSuite.Destroy;
begin
    FreeAndNil(FInvalidTests);
    inherited;
end;

procedure TMvTestSuite.AddKnownInvalidTest(ATestName: string);
begin
    if not Assigned(FInvalidTests) then
      FInvalidTests := TStringList.Create;

    FInvalidTests.Add(ATestName);
end;


procedure TMvTestSuite.RunTest(TestResult: TTestResult);
begin
    TestResult.AddListener(TIShouldRunTestListener.Create(FInvalidTests));
    inherited;
end;

For tests, that are known to fail, you can implement ITestListener interface and override ShouldRunTest.

The advantage of this solution (compared to DisableTests by @WarrenP) is, that it does not affect the check boxes, that determine which tests to run.

I use a common base class TMvTestSuite for my test suites, that adds a function AddKnownInvalidTest that can be called in the constructor (e.g. after AddTest):

type

    /// Listener that only implements ShouldRunTest
    TIShouldRunTestListener = class(TInterfacedObject, ITestListener)
    private
        FInvalidTests: TStrings;
        function IsInvalidTest(ATest: ITest): Boolean;
    protected
        function  ShouldRunTest(ATest: ITest):Boolean;
        procedure Status(test :ITest; const Msg :string);
        procedure TestingStarts;
        procedure StartTest(test: ITest);
        procedure AddSuccess(test: ITest);
        procedure AddError(error: TTestFailure);
        procedure AddFailure(Failure: TTestFailure);
        procedure EndTest(test: ITest);
        procedure TestingEnds(testResult :TTestResult);
    public
        constructor Create(AInvalidTests: TStrings);
    end;

    TMvTestSuite = class abstract(TTestSuite, ITestSuite, ITest)
    private
        FInvalidTests: TStrings;
    protected
        procedure RunTest(TestResult: TTestResult); override;
    protected
        procedure AddKnownInvalidTest(ATestName: string);
    public
        destructor Destroy; override;
    end;

implementation

constructor TIShouldRunTestListener.Create(AInvalidTests: TStrings);
begin
    FInvalidTests := AInvalidTests;
end;

function TIShouldRunTestListener.IsInvalidTest(ATest: ITest): Boolean;
begin
    Result := Assigned(FInvalidTests)
      and (FInvalidTests.IndexOf(ATest.Name) <> -1);
end;

function TIShouldRunTestListener.ShouldRunTest(ATest: ITest): Boolean;
begin
    Result := not IsInvalidTest(ATest);
end;

procedure TIShouldRunTestListener.AddError(error: TTestFailure);
begin
end;

procedure TIShouldRunTestListener.AddFailure(Failure: TTestFailure);
begin
end;

procedure TIShouldRunTestListener.AddSuccess(test: ITest);
begin
end;

procedure TIShouldRunTestListener.EndTest(test: ITest);
begin
end;

procedure TIShouldRunTestListener.StartTest(test: ITest);
begin
end;

procedure TIShouldRunTestListener.Status(test: ITest; const Msg: string);
begin
end;

procedure TIShouldRunTestListener.TestingEnds(testResult: TTestResult);
begin
end;

procedure TIShouldRunTestListener.TestingStarts;
begin
end;

{ TMvTestSuite }

destructor TMvTestSuite.Destroy;
begin
    FreeAndNil(FInvalidTests);
    inherited;
end;

procedure TMvTestSuite.AddKnownInvalidTest(ATestName: string);
begin
    if not Assigned(FInvalidTests) then
      FInvalidTests := TStringList.Create;

    FInvalidTests.Add(ATestName);
end;


procedure TMvTestSuite.RunTest(TestResult: TTestResult);
begin
    TestResult.AddListener(TIShouldRunTestListener.Create(FInvalidTests));
    inherited;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文