DUnit: How to run tests?
Asked Answered
Z

3

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?

Zebrawood answered 22/3, 2010 at 16:7 Comment(2)
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.Trudytrue
@Monk: The downside of that is that i have code spread across multiple classes, or worse multiple files.Zebrawood
A
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

Acetaldehyde answered 22/3, 2010 at 16:22 Comment(13)
WHy is this better than having a project group with MyApp and MyApp_Tests as two separate projects?Darryl
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.Zebrawood
A switchable 'real' and 'test' mode project? Code smells ahead :PAnstus
@Ian Boyd: Seems, there is the way to do it as desgined or the "lollipop lane".Met
@Uwe i'm more interested in ease of use, rather than how others use it.Zebrawood
@mjustin Your idea sounds interesting - one project. How would one do it?Zebrawood
@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. BriAcetaldehyde
@Brian Frost: No one hinders you to copy the TextProject.exe to that machine and run it.Met
@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.Met
+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.Ovariectomy
+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.Ferrand
@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? :-)Darryl
@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.Zebrawood
M
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.

Met answered 22/3, 2010 at 16:16 Comment(7)
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.Darryl
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.Zebrawood
add it as another project to the same project group. Then you can do a build all from here and so forth...Dominickdominie
@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?Zebrawood
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...Zebrawood
@Ian. Yes you can run run both projects in the debugger provided they have been both compiled before starting the 1st one.Dominickdominie
And the reason i can't do this is #2509203Zebrawood
A
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

Acetaldehyde answered 22/3, 2010 at 16:22 Comment(13)
WHy is this better than having a project group with MyApp and MyApp_Tests as two separate projects?Darryl
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.Zebrawood
A switchable 'real' and 'test' mode project? Code smells ahead :PAnstus
@Ian Boyd: Seems, there is the way to do it as desgined or the "lollipop lane".Met
@Uwe i'm more interested in ease of use, rather than how others use it.Zebrawood
@mjustin Your idea sounds interesting - one project. How would one do it?Zebrawood
@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. BriAcetaldehyde
@Brian Frost: No one hinders you to copy the TextProject.exe to that machine and run it.Met
@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.Met
+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.Ovariectomy
+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.Ferrand
@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? :-)Darryl
@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.Zebrawood
A
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)

Anstus answered 22/3, 2010 at 18:56 Comment(2)
Now someone just has to write it ;)Zebrawood
Maybe one of my supporters :) delphi.uservoice.com/forums/4432-general/suggestions/…Anstus

© 2022 - 2024 — McMap. All rights reserved.