Search code examples
delphiunit-testingdunit

How can I disable DUnit tests by name programmatically?


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.


Solution

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