10

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

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

alt text

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • As an aside, you'll find that your business logic is easier to unit test if you separate it out into classes, rather than trying to unit test your UI event handlers directly. – Incredulous Monk Mar 23 '10 at 05:45
  • @Monk: The downside of that is that i have code spread across multiple classes, or worse multiple files. – Ian Boyd Mar 25 '10 at 19:55

3 Answers3

12

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.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • 1
    Exactly. Test Case code does not belong IN YOUR APPLICATION. Your test case project contains your unit tests. You create a new unit test application, you add your application code to the uses-clause of your unit test. – Warren P Mar 22 '10 at 16:57
  • 2
    Is there a way to run the test project without having to switch away from the real project? i'd like to treat tests similar to a `Syntax Check`, `Build Project` or `Build All`. – Ian Boyd Mar 22 '10 at 17:08
  • 4
    add it as another project to the **same project group**. Then you can do a build all from here and so forth... – Francesca Mar 22 '10 at 17:26
  • @François Is there a way to run the tests contained in the other "test project" while i'm in the project i'm developing? – Ian Boyd Mar 22 '10 at 21:10
  • Seems to me that people are concerned about test code being in the executable, rather than being logically separate - and used to test the code. i'm okay with DUnit working it so that the test cases are compiled away... – Ian Boyd Mar 22 '10 at 21:16
  • @Ian. Yes you can run run both projects in the debugger provided they have been both compiled before starting the 1st one. – Francesca Mar 23 '10 at 00:15
  • And the reason i can't do this is http://stackoverflow.com/questions/2508703/delphi-how-to-set-the-default-project-in-a-project-group – Ian Boyd Mar 25 '10 at 19:55
6

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

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Brian Frost
  • 13,334
  • 11
  • 80
  • 154
  • WHy is this better than having a project group with MyApp and MyApp_Tests as two separate projects? – Warren P Mar 22 '10 at 16:57
  • If i have a separate project with test code, then wouldn't have a separate project? i really don't want to have to flip between projects. i don't want to have to flip a drop-down between "Real Project" and "Test Project". If that means i have test code embedded in my final executable: then so be it. – Ian Boyd Mar 22 '10 at 17:07
  • 5
    A switchable 'real' and 'test' mode project? Code smells ahead :P – mjn Mar 22 '10 at 18:48
  • @Ian Boyd: Seems, there is the way to do it as desgined or the "lollipop lane". – Uwe Raabe Mar 22 '10 at 21:08
  • @Uwe i'm more interested in ease of use, rather than how others use it. – Ian Boyd Mar 22 '10 at 21:12
  • @mjustin Your idea sounds interesting - one project. How would one do it? – Ian Boyd Mar 22 '10 at 21:12
  • 3
    @Warren P: Because there are (nasty) situtations where your app is on a customer's machine, he (She) is not looking over your shoulder and you bless that you can use some low-level built in tools such as your DUnit tests to assure you that the basics are ok. Bri – Brian Frost Mar 22 '10 at 21:27
  • 3
    @Brian Frost: No one hinders you to copy the TextProject.exe to that machine and run it. – Uwe Raabe Mar 23 '10 at 06:52
  • @Ian Boyd: I have done something like a "switchable" project for automating the screenshots. I use a special build configuration for that. It's like a debug build, the customer will (should) never see it. – Uwe Raabe Mar 23 '10 at 06:58
  • +1 for being able to run your tests from within a production environment. @Uwe you might be allowed or able to copy your test executable into the production environment. – Frank Shearar Mar 23 '10 at 08:19
  • +1 for the idea too! There are many cases where it is annoying or problematic to get the customer to get another EXE and run it. – stg Mar 23 '10 at 15:21
  • @Brian: Your customer is lucky to have a developer working for them who understands the value of unit testing. Isn't it ironic that when they have one of the good-ones (ie you), you get the runaround? :-) – Warren P Mar 23 '10 at 19:52
  • @Uwe Raabe "No one hinders you to copy the TextProject.exe to that machine and run it." i have two government agencies hindering me from copying a `TestProject.exe`. The more i can jam into the one executable. i already embed a `IXMLHttpRequest` test app (when they have a proxy in the way), and a **Query Analyzer**. Running tests remotely over **CoPilot** is one more good thing. – Ian Boyd Jun 09 '11 at 21:19
2

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)

mjn
  • 36,362
  • 28
  • 176
  • 378