gambas-source-code/README.md

146 lines
5.1 KiB
Markdown
Raw Normal View History

2016-09-22 19:37:43 +02:00
# gb.deg.unittest A Gambas Unittest
2016-09-25 12:48:35 +02:00
A Gambas component for unittesting and test-driven programming. Forked and inspired from quite an old program: [COMUnit](http://comunit.sourceforge.net) and mainly JUnit. Currently alpha state. With an unittest component one can develop software in a test-driven matter and is able to ensure that on refactoring the desired results of methods and classes stay the same.
2016-09-22 22:55:54 +02:00
2016-09-23 22:05:11 +02:00
Scroll down to understand how it works.
2016-09-22 19:37:43 +02:00
## Runner
2016-09-23 22:05:11 +02:00
Here you can see the Unittest tests itself. The testclasses (here called TestContainer) have to produce some failures and one error, to prove all is working fine.
2016-09-22 22:55:54 +02:00
2016-09-22 19:37:43 +02:00
![Unittest Runner](runner-screen.png)
## Tracer
2016-09-23 22:05:11 +02:00
The Trace tab gives a quick overview:
2016-09-22 22:55:54 +02:00
2016-09-22 19:37:43 +02:00
![Unittest Tracer](tracer-screen.png)
## How it works
2016-09-23 22:23:19 +02:00
It's a component. To make it work, you have to generate an installation package for your distribution with Gambas3 (it's proved to work for Version 3.8 upwards) and install it on your Linux system. After that you can use it in all your projects as a component.
2016-09-22 19:37:43 +02:00
2016-09-23 22:05:11 +02:00
The following example you also find in [this simple Gambas project](unittesthelloworld-0.0.1.tar.gz).
2016-09-22 19:53:57 +02:00
2016-09-23 08:12:43 +02:00
### Example TestContainer
2016-09-22 19:37:43 +02:00
2016-09-23 22:05:11 +02:00
Start by creating a class with a name like "_Test", for example "_TestHelloWorld". This class contains one or more public testmethod(s). It has to inherit from ATestContainer and it has to be exported to ensure that Unittest will recognize it. The trailing underscore ensures that the Gambas interpreter hides these classnames, even when they are exported. But you can take any other name for it. This class is the so called TestContainer:
2016-09-22 19:37:43 +02:00
----
2016-09-23 08:12:43 +02:00
' Gambas class file
2016-09-23 15:03:03 +02:00
''' TestContainer _TestHelloWorld
2016-09-22 19:37:43 +02:00
Export
Inherits ATestContainer
2016-09-23 08:12:43 +02:00
2016-09-22 19:37:43 +02:00
Public Sub TestHelloWorld()
Me.Result.AssertEqualsString("Hello World", Hello.World(), "Strings should be equal")
End
----
### File Unittests
In your project's path a file called "Unittests" must exist. This file contains one line per TestContainer with the TestContainer's name:
# File Unittests
_TestHelloWorld
If it does not exist, the TestContainers are not found.
2016-09-22 19:37:43 +02:00
### Module(Function) to test:
2016-09-23 08:12:43 +02:00
To make it work, we need a funktion to test. So we create a function "World" in a module "Hello" in our project:
2016-09-22 19:37:43 +02:00
----
' Gambas module file
2016-09-23 08:12:43 +02:00
''' Module is named "Hello"
2016-09-22 19:37:43 +02:00
Public Function World() As String
Dim w As String
w = "Hello World"
Return w
End
----
### Invite Unittest
2016-09-23 22:05:11 +02:00
The simple way to execute the Unittest is to create another module, name it "Test" or something more interesting and make it a Gambas Startclass:
2016-09-22 19:37:43 +02:00
----
'Module Test
'Starts the Unittest
Public Sub Main()
Dim U as New Unittest
U.Test
End
----
2016-09-23 22:05:11 +02:00
If you did all this correctly and now hit < F5 >, Gambas will execute the startfunction in module Test, which works through the method(s) of our TestContainer and presents the test result in the console:
2016-09-22 19:37:43 +02:00
----------------------- Test Results -----------------------
1 Tests done
------------------------------------------------------------
No Errors
No Failures
------------------------- Test End -------------------------
Success!
----
2016-09-23 22:23:19 +02:00
Sooo ... and if you want to see the beautiful form, alter the startclass (erm ... startmodule)
2016-09-22 22:55:54 +02:00
2016-09-22 19:37:43 +02:00
----
Public Sub Main()
Dim U As New Unittest
2016-09-22 22:55:54 +02:00
'U.Test is now replaced by
2016-09-22 19:37:43 +02:00
U.ShowTestForm
End
----
2016-09-23 22:23:19 +02:00
If you now hit < F5 > you'll see the testform where you can choose and run your tests. Afterwards have a look at the Trace tab:
2016-09-22 19:37:43 +02:00
![Unittest Tracer](trace-helloworld.png)
## Unittesting with gb.deg.unittest
Look around by positioning the cursor on "Unittest" and hit < F1 >. The relevant methods to test your code are in the class TestResult:
AddError AddFailure AddTrace Assert AssertEmpty AssertEqualsFloat AssertEqualsLong AssertEqualsObject AssertEqualsString AssertEqualsVariant AssertError AssertExists AssertFalse AssertNotEmpty AssertNotNull
Dig it out!
2016-09-23 08:12:43 +02:00
## Test fixture
2016-09-23 22:23:19 +02:00
Sometimes it is neccessary to create a "fixture", a special environment for a test or a couple of tests, and to destroy this environment after the test is done. For example a database connection should be established, some tables for testing should be created and this has to be reverted afterwards. This can be done with Setup... and Teardown... functions inside the TestContainer.
2016-09-23 08:12:43 +02:00
### Sub SetupEach() and Sub TeardownEach()
2016-09-22 19:37:43 +02:00
2016-09-23 22:23:19 +02:00
You can create methods with these names to create an environment for each testmethod before it is invoked and to destroy it afterwards. If you have five testmethods inside your TestContainer these functions will be invoked five times, SetupEach() before each testmethod, TeardownEach() after each testmethod. Got it?
2016-09-22 19:37:43 +02:00
2016-09-23 08:12:43 +02:00
### Sub SetupContainer() and Sub TeardownContainer()
2016-09-22 19:37:43 +02:00
2016-09-23 22:23:19 +02:00
You can create methods with these names to create an environment for all testmethods inside a TestContainer, in the beginning SetupContainer() is invoked and after all testmethods inside the testclass are done you can destroy the environment with TeardownContainer().
2016-09-22 19:37:43 +02:00