Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions Example/TestForVersion_0.1.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Testing script for testing the new changes introduced in UnitTestingApp script on version 0.1.1
*/

// jshint esversion: 8
if (typeof require !== 'undefined') {
UnitTestingApp = require('./UnitTestingApp.js');
}

// Function to test
function addTwoValues (a, b) {
return a + b;
}

/*****************
* TESTS
* Taking the sources files from: https://github.com/WildH0g/UnitTestingApp
*****************/

/**
* Runs the tests; insert online and offline tests where specified by comments
* @returns {void}
*/
function runTests() {
const test = new UnitTestingApp();
test.enable();
test.clearConsole();
test.runInGas(true);

/************************
* Run Local Tests Here
************************/

test.printHeader("Testing addTwoValues using assertEquals");
// Testing new method: assertEquals, printSummary and printSubHeader using addTwoValue function with default levelInfo
// We haven´t set levelInfo, so we are using the default value: 1
test.printSubHeader("Using default Level Info value (1) for 'addTwoValues' function");
test.assertEquals(() => addTwoValues(1,2),3); // PASS
test.assertEquals(() => addTwoValues(2,2),4, "Valid case: 2 + 2 = 4"); // PASS with message
test.assertEquals(() => addTwoValues(1,2),4); // FAIL
test.assertEquals(() => addTwoValues(1,2),4, "Expected to fail, because 1+2 != 4");
test.printSummary(); // It should print final result and statistics (two lines)

// Testing the same tests but in silent mode (LevelInfo = 0)
test.printSubHeader("Testing the output setting levelInfo = 0"); // It logs out, because we haven't changed LevelInfo yet
test.setLevelInfo(0); // 0-Only summary result, 1-Detail results
// Because LevelInfo = 0 we use the console for traceability purpose
console.log("LevelInfo = " + test.getLevelInfo());
test.assertEquals(() => addTwoValues(1,2),3);
test.assertEquals(() => addTwoValues(2,2),4, "Valid case: 2 + 2 = 4");
test.assertEquals(() => addTwoValues(1,2),4);
test.assertEquals(() => addTwoValues(1,2),4, "Expected to fail, because 1+2 != 4");
test.printSummary(); // Only summary result: Some test failed
console.log("Reset counters");
test.resetTestCounters();
console.log("Shows only the summary line, the counters are reseted so no tests, therefore all test passed");
test.printSummary(); //Shows only summary line

// Testing the existing assert function work as expected
test.printHeader("Testing addTwoValues using assert");
test.printSubHeader('Default level Info');
test.setLevelInfo(1);
test.printSubHeader("LevelInfo = " + test.getLevelInfo());
test.assert(() => addTwoValues(1,2) == 3, "Valid case: 1 + 2 = 3");
test.assert(() => addTwoValues(1,2) == 4, "Invalid case: 1 + 2 != 4");
test.printSubHeader("Expected to log out two lines: statistics and summary");
test.printSummary();

test.printSubHeader("Testing under silent mode: one test passed, one failed");
test.setLevelInfo(0);
console.log("LevelInfo = " + test.getLevelInfo());
test.assert(() => addTwoValues(1,2) == 3, "Valid case: 1 + 2 = 3");
test.assert(() => addTwoValues(1,2) == 4, "Invalid case: 1 + 2 != 4");
console.log("Expected to see some test failed");
test.printSummary();

// Reset the counters. For testing all test passed under silent mode
console.log("Testing the case all test passed with silent mode");
console.log("Reseting the counters");
test.resetTestCounters();
console.log('Testing with assert, under silent mode: one test executed and passed');
test.assert(() => addTwoValues(1,2) == 3, "Valid case");
console.log("Printing the summary line only: all test passed");
test.printSummary();
console.log("Changing the level info to 1");
test.setLevelInfo(1);
test.printSubHeader("Showing now printSummary with two lines for the previous set of tests");
test.printSummary();

test.printHeader("Testing enable = false, the print-family functions print no information");
test.disable();
test.printHeader("No expected output");
test.printSubHeader("No expected output");
test.printSummary();

}
123 changes: 118 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if(test.isEnabled) {

test.disable(); // tests will not run below this line
```
**Note:** If tests are disable, print-family function will no produce any output regardless of the level of information we have configured to show. See section: *Control Level of Information to log out* for more information.

### Choosing the Environment with runInGas(Boolean)

Expand All @@ -48,6 +49,79 @@ test.runInGas(true);

Then we have the actual built-in testing methods, `assert()`, `catchErr()` and `is2dArray()`.

### Control Level of Information to log out

The function `setLevelInfo(value)` controls the level information to be reported through the console. If `value`is equal to `0`, it runs in silent mode, i.e. no information will be reported. The only exception from print-family functions is `printSummary()` that under this mode just logs out a single summary line. For example if no errors found, the output will be:

```ALL TESTS ✔ PASSED```

if at least one test failed then it logs out the following information:

```❌ Some Tests Failed```

This setup is usufull for large tests where we just want to add some incremental test and to have just a minimal output about the overall testing result.

Here is how the level of information can be specified for silent mode:

```javascript
test.setLevelInfo(0);
```
If value is `1`(default value) it means trace information will log out the result per each test, indicating if the test fails or passed. Depending on the specific testing function it will log out different information, for example, let's says we want to test the following custom function:

```javascript
function addTwoValues (a, b) {
return a + b;
}
```

then invoking the following assert function:

```javascript
test.assertEquals(() => addTwoValues(1,2),3); // where expected result is 3
```

will return

```
✔ PASSED
```

on contrary, if we invoke the function with a wrong expected result:

```javascript
test.assertEquals(() => addTwoValues(1,2),4); // where expected result is 4
```

will return

```
❌ FAILED: 3 != 4
```

As you can see there is no need to specify the message, it shows where is the problem. If we invoke `assertEquals` including its optional input argument `message`, then the output information will be on each case the information specificied by this input argument

```javascript
test.assertEquals(() => addTwoValues(1,2),3, "Expected result: 1+2 = 3");
test.assertEquals(() => addTwoValues(1,2),4, "Wrong result because 1+2 should be equal to 3");
```

The output will be:

```
✔ PASSED: Expected result: 1+2 = 3
❌ FAILED: Wrong result because 1+2 should be equal to 3
```

If we are using `assert` that has a mandatory `message`as input argument, the message will be printed as testing result.

When using `printSummary()` with level info is equal to `1`, it logs out an additional line providing statistics about testing results:

```
TOTAL TESTS= 1, ❌ FAILED=0, ✔ PASSED=1
ALL TESTS ✔ PASSED
```
Indicating that `1`test passed and `0` test failed and the total tests executed

### assert(condition, message)

`assert()` is the main method of the class. The first argument that it takes is the condition and it checks whether the condition is truthy or falsy. The condition can either be a boolean value or a function that returns such a condition, function being the preferred approach for error catching reasons. If the condition is truthy, it logs out a “PASSED” message, otherwise it logs out a “FAILED” message. If you pass in a function that throws an error, the method will catch the error and log out a “FAILED” message. For example:
Expand All @@ -61,6 +135,16 @@ test.assert(() => num % 2 === 0, `Number ${num} is even`’);
test.assert(() => num > 10, `Number ${num} is bigger than 10`);
// logs out FAILED: Number 2 is bigger than 10
```
### assertEquals(fun, expectedResult, message)
`assertEquals`is another assert function, that helps to verify the result with respect the expected value. The first argument is the function we would like to evaluate, the second argument is the expected result. If returned value of the `fun`is not equal to `expectedResult` it fails, otherwise passed. Here is how to validate javascript standard `max()`function:

```javascript
test.assertEquals(() => Math.max(1,2),2); # Expected result is 2 (equal to actual result)
test.assertEquals(() => Math.max(1,2),1); # Expected result is 1 (different than actual result)
test.assertEquals(() => Math.max(1,2),2, "Correct result the max of 1,2 is 2"); # Using option input argument message
```
The last case specifies the message to log out as testing result regardless of the test result, the same message will be log out.


### catchErr(condition, expectedErrorMessage, message)

Expand Down Expand Up @@ -94,20 +178,49 @@ test.is2dArray(values, ‘values is an array of arrays’); // logs out PASSED

Then there are a couple of helper methods, `printHeader()` and `clearConsole()`.

### printHeader(headerStr)
### Print-family Functions

**Note**: The level of information shows by print-family functions wil depend on the level of information the user specified via `setLevelInfo(vaue)`, or if no value was specified it assumes the level of infomration is `1`. See section: *Control Level of Information to log out* for more information.

Just helps with readability by printing a header in the console like this:
The `printHeader()` function just helps with readability by printing a header in the console like this. It can be used for printing for example the title of the testing set. Here the expected result under `1` level of information:

```javascript
test.printHeader(‘Offline tests’);
/*
```
Logs out the following:
```
*********************
* Offline tests
*********************
*/
```

There also a second print header function: `printSubHeader(text)`, usefull to log out a sub header as a single line with prefix `**`. Here the output under level of information equal to `1`:

```javascript
test.printSubHeader(‘Testing valid cases...’);
```
logs out:
```
** Testing valid cases...
```
There is a third print function: `printSummary()`, that logs out a summary of testing results (depending on the level of information we want to show)

```javascript
test.printSummary();
```
If we ran 20 tests, where there is one failed test, under level of information equal to `1`, the result will be:
```
TOTAL TESTS= 20, ❌ FAILED=1, ✔ PASSED=19
ALL TESTS ✔ PASSED
```
Similarly if the level of information is equal to `0`, the output will be:
```
ALL TESTS ✔ PASSED
```

### resetTestCounters()
The `resetTestCounters()` is usefull for reseting testing counters (`_nTests`, `_nFailTests`, `_nPassTests`, attributes of the class `UnitTestingApp`), the function `printSummary()` will log out information depending on the overall testing results based on such attributes. We can use this function to reset testing counters after a running a set of tests, so we can print a summary information per set of tests using `printSummary()`.


### clearConsole()

Expand Down Expand Up @@ -183,4 +296,4 @@ function runTests() {

## Current Version

0.1.0
0.1.1
Loading