Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:17:23+00:00 2026-05-31T19:17:23+00:00

I am creating unit tests with DUnit. I have a class that takes quite

  • 0

I am creating unit tests with DUnit. I have a class that takes quite a long time to initialize.

I derive a class TMyTestSetup from TTestSetup and override its Setup method. This SetUp method is only called once for all the tests in my TTestCase. I put the Initialization process in the TMyTestSetup.SetUp routine to increase performance.

My problem is how can I access the object I want to initialize, which is a field of my TMyTest in the TestSetup class? Is the only way to do it declaring it globally?

untested short example:

TMyTestSetup = class(TTestSetup)
  protected
    procedure SetUp; override;
end;

TMyTest = class(TTestcase)
public
    fTakes4Ever2Init : TInits4Ever2Init;
published
  procedure Test1;     
end;

implementation

procedure TMyTestSetup.Setup;
begin
   // How can I access fTakes4Ever2Init from here?
  fTakes4Ever2Init.create // This is the call that takes long
end;

procedure TMyTest.Test1;
begin
  fTakes4Ever2Init.DoSomething;
end;

initialization
  RegisterTest(TMyTestSetup.Create(TMyTest.Suite));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:17:24+00:00Added an answer on May 31, 2026 at 7:17 pm

    The trick is to use a public class variable in the TMyTestSetup class.

    Like this (tested and working, complete) example:

    unit TestTestUnit;
    
    interface
    
    uses
      TestFramework, TestExtensions;
    
    type
      TInits4Ever2Init = class
      private
        FValue: integer;
      public
        constructor Create;
        procedure   DoSomething1;
        procedure   DoSomething2;
        procedure   DoSomething3;
      end;
    
    type
      TMyTestSetup = class(TTestSetup)
      public class var
        fTakes4Ever2Init: TInits4Ever2Init;
      protected
        procedure SetUp; override;
      end;
    
      TMyTest = class(TTestCase)
      published
        procedure Test1;
        procedure Test2;
        procedure Test3;
      end;
    
    implementation
    
    uses
      SysUtils, Windows;
    
    { TMyTestSetup }
    
    procedure TMyTestSetup.Setup;
    begin
      fTakes4Ever2Init := TInits4Ever2Init.create; // This is the call that takes long
    end;
    
    { TMyTest }
    
    procedure TMyTest.Test1;
    begin
      TMyTestSetup.fTakes4Ever2Init.DoSomething1;
    end;
    
    procedure TMyTest.Test2;
    begin
      TMyTestSetup.fTakes4Ever2Init.DoSomething2;
    end;
    
    procedure TMyTest.Test3;
    begin
      TMyTestSetup.fTakes4Ever2Init.DoSomething3;
    end;
    
    { TInits4Ever2Init }
    
    constructor TInits4Ever2Init.Create;
    begin
      inherited Create;
    
      // FValue and Format('%p, %d', [Pointer(Self), FValue])) are to confirm
      //   that we are talking to the same object for all the tests,
      //   but that the object is different each time we run the test suite.
    
      Randomize;
      FValue := Random(10000);
    
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.Create: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    procedure TInits4Ever2Init.DoSomething1;
    begin
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.DoSomething1: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    procedure TInits4Ever2Init.DoSomething2;
    begin
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.DoSomething2: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    procedure TInits4Ever2Init.DoSomething3;
    begin
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.DoSomething3: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    initialization
      RegisterTest(TMyTestSetup.Create(TMyTest.Suite));
    end.
    

    As the comments in the sample indicate, I have used a randomised private variable, and some debug trace output, to confirm that each test call with the test suite is to the same copy of the target object, but that we are getting a different copy of the target object each time the test suite is run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been creating Unit tests like crazy and find that I'm often having
We have some unit tests which work with performance counters (specifically creating new categories)
Does anyone have any frameworks/apps/methodologies for creating Unit tests with Oracle?. I'm using Oracle
I have a class which has some unit tests, but when I am running
I've just started creating Unit Tests for my code again. I have had PHPUnit
After some hours creating a vb.net helper class for unit-tests and integrating moq into
I have some written a number of unit tests that test a wrapper around
Recently I was trying to add unit tests to an existing binary by creating
I am setting up unit tests to run methods that should generate an NSError.
I have never written unit tests before, for various reasons. I have a chance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.