| ... | @@ -3,4 +3,52 @@ This page will explain how to make simple unit tests and why it is good to use. |
... | @@ -3,4 +3,52 @@ This page will explain how to make simple unit tests and why it is good to use. |
|
|
### 1 Sourcecode
|
|
### 1 Sourcecode
|
|
|
The sourcecode for the examples are found in the [language/matlab](https://gitlab.itn.liu.se/rasmus.ringdahl/getting_started/tree/master/languages/matlab) in the repository.
|
|
The sourcecode for the examples are found in the [language/matlab](https://gitlab.itn.liu.se/rasmus.ringdahl/getting_started/tree/master/languages/matlab) in the repository.
|
|
|
|
|
|
|
|
TODO: write something. |
|
### 2 Purpose with unit testing
|
|
\ No newline at end of file |
|
Unit testing is used for test functionality in the sourcecode. Small programs with simple classes and methods are easy to test while coding but when the starts to be complex it is hard to know that the code works as intended.
|
|
|
|
|
|
|
|
To solve this issue a framework for testing is needed. Unit tests is a common way to test the code. Unit test work by creating one or many test cases that checks that a method returns a predefined expected answer.
|
|
|
|
|
|
|
|
As the code base increases the power of unit testing grows stronger. The unit tests are often not removed so the number of tests increases, when a bug is introduced later that breaks one of the test this will be notified directly and the bug can be resolved before it reaches the production code.
|
|
|
|
|
|
|
|
### 3 Unit test syntax
|
|
|
|
Matlab has internal functions for unit tests and the test cases should be placed in so the program can find the functions that will be tested. The name are prefered to be named "test" followed with the name of the tested file e.g. testCalculateTriangleArea.m.
|
|
|
|
|
|
|
|
The test case files need to have a main function tat calls `functiontests(localfunctions)`
|
|
|
|
|
|
|
|
In each test case one or more things can be tested by using verify. Following lines show an example from testCalculateTriangleArea.m.
|
|
|
|
|
|
|
|
|
|
|
|
```matlab
|
|
|
|
function tests = testCalculateTriangleArea( testCase )
|
|
|
|
%TESTCALCULATETRIANGLEAREA Test case for calculateTriangleArea.
|
|
|
|
|
|
|
|
tests = functiontests(localfunctions);
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
% This tests if the calculateTriangleArea method returns the right value.
|
|
|
|
function testAreaCalculation(testCase)
|
|
|
|
% Input values.
|
|
|
|
base = 10*rand;
|
|
|
|
height = 10*rand;
|
|
|
|
|
|
|
|
% Expected value.
|
|
|
|
expectedOutput = base * height / 2;
|
|
|
|
|
|
|
|
% Preforming the test.
|
|
|
|
actualOutput = calculateTriangleArea(base, height);
|
|
|
|
|
|
|
|
verifyEqual(testCase,actualOutput, expectedOutput, 'RelTol',0.01)
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
If the ´calculateTriangleArea()´ gives the same value as `expectedOutput` then this test will pass othervise it will fail. The assert has a error margin of 0.01.
|
|
|
|
|
|
|
|
When running the RunAllTests.m from the getting_started/languages/matlab the output from the tests will look like this.
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
In this case the test case testAreaCalculation fails. |
|
|
|
\ No newline at end of file |