delphi 如何通过编程方式按名称禁用DUnit测试?

nhhxz33t  于 2023-04-20  发布在  其他
关注(0)|答案(2)|浏览(100)

对于集成测试,我创建了一个DUnit测试套件,它对第三方组件(消息代理)的每个版本运行一次。不幸的是,一些测试总是失败,因为某些版本的测试组件中存在已知的错误。
这意味着测试套件永远不会100%完成。然而,对于自动化测试,需要100%的成功分数。DUnit没有提供现成的方法来通过名称禁用测试套件中的测试。

taor4pac

taor4pac1#

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

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;

示例用法(在Setup方法中创建TStringlist“Excludes”):

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;
gv8xihay

gv8xihay2#

对于已知失败的测试,您可以实现ITestListener接口并覆盖ShouldRunTest
我为我的测试套件使用了一个公共基类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;

相关问题