-
Notifications
You must be signed in to change notification settings - Fork 14
Smoketest Getting Started
This is a rapid walk-through guide to creating a first test project with Smoketest. More detailed documentation about various aspects of the framework may be found elsewhere on this wiki.
For any Smoketest project you will need the following on either the project search path or IDE library path:
- $(deltics)+inc
- $(deltics)\rtl
- $(deltics\smoketest
It is recommended that you add the rtl
and +inc
paths to your IDE library path and smoketest
to your test projects.
Currently there are no templates or IDE experts provided for Smoketest so you will have to create your project and test cases using appropriate selections from the default file types in Delphi.
To create a new test project, start with a Console application. Remove the $APPTYPE CONSOLE
declaration and edit the DPR so that you have a project source similar to the following:
program MyFirstTestProject;
uses
Deltics.Smoketest;
begin
Smoketest.Ready;
end.
Don't forget to add whichever paths you have not already added to the IDE library path to the project options search path.
Test cases are classes derived from the Smoketest TTestCase class. For very simple test projects you may choose to implement your test cases in the test project file itself, but usually you will create them in units of their own, which is what we shall do now.
Create and add a new unit to the project and make any necessary changes so that it looks something like this:
unit MyFirstTestCase;
interface
uses
Deltics.Smoketest;
type
TMyFirstTestCase = class(TTestCase)
end;
implementation
initialization
Smoketest.Add([TMyFirstTestCase]);
end.
This defines a new test case and adds it to the Smoketest project in the initialization
section. To have our test included in the project we simply add the test case unit to the project uses list:
program MyFirstTestProject;
uses
Deltics.Smoketest,
MyFirstTestCase;
begin
Smoketest.Ready;
end.
Now we can implement the tests that our test case is to perform.
Returning to the MyFirstTestCase
unit, tests are written as published, parameterless methods on the test case class. Since the visibility of methods is published by default, this means we can simply add a test method to the test case we previously created:
type
TMyFirstTestCase = class(TTestCase)
procedure MyFirstTest;
end;
And the implementation body of that test method:
procedure TMyFirstTestCase.MyFirstTest;
begin
end;
Tests all start with a call to the test case Test() method. This method optionally accepts a label which may be used to describe or identify the test. If no label is provided then a default will be assumed.
The Test() method returns an interface with a number of methods for capturing a value to be tested. Normally this method will be called Expect() and returns a further interface. The interface returned by the Expect() method provides all of the tests that are appropriate to be performed on values of the type of value being tested.
This means that the tests available for an Integer value are quite different from those for a String value, for example.
You can test as many values as you wish in any test method. For this quick-start guide we shall include two tests in our test method, one for an integer and the other for a string:
procedure TMyFirstTestCase.MyFirstTest;
begin
Test('The meaning of life').Expect(7 * 6).Equals(42);
Test('Book title').Expect('Hitchhiker''s Guide to The Galaxy").BeginsWith('Hitchhiker''s');
end;
If you run this test project you will get a single test case. Double-click the test case to expand it and the reveal the test methods it contains. If you run the test and then select this test method, you should see two successful test results in the test output view.
Congratulations! That is your first Smoketest project. And it passed with flying colours! :)