views:

303

answers:

3

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?

+9  A: 

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
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
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
add it as another project to the **same project group**. Then you can do a build all from here and so forth...
François
@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
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
@Ian. Yes you can run run both projects in the debugger provided they have been both compiled before starting the 1st one.
François
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
+3  A: 

I agree with Uwe Raabe, but sometimes it can be useful to have a 'hidden' limk 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. Brian.

Brian Frost
WHy is this better than having a project group with MyApp and MyApp_Tests as two separate projects?
Warren P
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
A switchable 'real' and 'test' mode project? Code smells ahead :P
mjustin
@Ian Boyd: Seems, there is the way to do it as desgined or the "lollipop lane".
Uwe Raabe
@Uwe i'm more interested in ease of use, rather than how others use it.
Ian Boyd
@mjustin Your idea sounds interesting - one project. How would one do it?
Ian Boyd
@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
@Brian Frost: No one hinders you to copy the TextProject.exe to that machine and run it.
Uwe Raabe
@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
+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
+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
@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
+2  A: 

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)

mjustin
Now someone just has to write it ;)
Ian Boyd
Maybe one of my supporters :) http://delphi.uservoice.com/forums/4432-general/suggestions/537663-dunit-ide-integration-for-failed-test
mjustin