DUnit: How to run tests?

2019-04-21 03:28发布

How do i run TestCase's from the IDE?

i created a new project, with a single, simple, form:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

end.

Now i'll add a test case to check that pushing Button1 does what it should:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses
    TestFramework;

type
  TForm1Tests = class(TTestCase)
  private
        f: TForm1;
  protected
     procedure SetUp; override;
     procedure TearDown; override;
  published
     procedure TestButton1Click;
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    //todo
end;

{ TForm1Tests }

procedure TForm1Tests.SetUp;
begin
  inherited;

    f := TForm1.Create(nil);
end;

procedure TForm1Tests.TearDown;
begin
    f.Free;
  inherited;
end;

procedure TForm1Tests.TestButton1Click;
begin
    f.Button1Click(nil);
    Self.CheckEqualsString('Hello, world!', f.Caption);
end;

end.

Given what i've done (test code in the GUI project), how do i now trigger a run of the tests? If i push F9 then the form simply appears:

alt text http://i39.tinypic.com/zsobhv.jpg

Ideally there would be a button, or menu option, in the IDE saying Run DUnit Tests:

alt text http://i44.tinypic.com/24wvipw.jpg

Am i living in a dream-world? A fantasy land, living in a gumdrop house on lollipop lane?

3条回答
唯我独甜
2楼-- · 2019-04-21 04:15

Adding a TestCase to the main project is not the way to go. You should create a separate TestProject (you can have it in the same ProjectGroup as the main project), add a TestCase and run.

查看更多
混吃等死
3楼-- · 2019-04-21 04:22

I agree with Uwe Raabe, but sometimes it can be useful to have a 'hidden' link within your app to run the DUnit GUI. I use:

TGUITestRunner.runRegisteredTests;

Call this from your button at the DUnit GUI will open for you to manually run and view test output.


For example, if you hold down a special key combination when opening the software's own "Control Panel", you get some advanced entries:

enter image description here

查看更多
三岁会撩人
4楼-- · 2019-04-21 04:26

I like the idea of having a 'Run DUnit tests' command in the IDE.

It could be implemented by checking for a DUnit project in the same folder, having the same name as the current project:

  • Project1.dpr -> the software under test
  • Project1.Tests.dpr => the DUnit test app

In this case, the IDE should enable the Run DUnit tests command.

  • After executing the tests, a list of all failed tests should be displayed which allows to jump to the source line where a test failed.

  • If tests caused memory leaks, a list of all leaks should be displayed which allows to jump to the source line where the memory leak has been created

(DUnit can be configured to detect memory leaks and fail tests when one has been found)

查看更多
登录 后发表回答