Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnitTest flags #514

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
92 changes: 92 additions & 0 deletions docs/docs/standard-lib/unittest.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,54 @@ Results:
- 0 method(s) were skipped.
```

#### Global
The above shows us how we can silence passing tests for a single group (class) of Unit Tests but, if we wish to silence
ass passing tests that would be a lot of verbosity added to every class. Instead we can use the global flag that is defined
as a class variable on the UnitTest parent class.

```
from UnitTest import UnitTest;

UnitTest.forceOnlyFailures = true; // Set global flag

def add(a, b) {
return a + b;
}

class Test < UnitTest {
testAddFunction(data) {
this.assertEquals(add(data["val1"], data["val2"]), data["expected"]);
}

// This will not be ran as a test as it's marked as a provider
testAddFunctionProvider() {
return [
{"val1": 1, "val2": 2, "expected": 3},
{"val1": 2, "val2": 2, "expected": 4},
{"val1": 3, "val2": 2, "expected": 5},
{"val1": 4, "val2": 2, "expected": 6},
{"val1": 5, "val2": 2, "expected": 7},
// This will fail
{"val1": 6, "val2": 2, "expected": 9},
];
}
}

Test().run();
```

Output:
```
file.du
testAddFunction()
Line: 17 - Failure: 8 is not equal to 9.

Results:
- 5 assertion(s) were successful.
- 1 assertion(s) were failures.
- 0 method(s) were skipped.
```

### Exit On Failure
Sometimes we may want our test suite to stop as soon as a test fails.
This is done very similarly to silencing passing tests.
Expand Down Expand Up @@ -306,6 +354,50 @@ file.du
Line: 17 - Failure: 3 is not equal to 5.
```

#### Global
The above shows us how we can silence passing tests for a single group (class) of Unit Tests but, if we wish to silence
ass passing tests that would be a lot of verbosity added to every class. Instead we can use the global flag that is defined
as a class variable on the UnitTest parent class.

```cs
from UnitTest import UnitTest;

UnitTest.forceExitOnFailure = true;

def add(a, b) {
return a + b;
}

class Test < UnitTest {
testAddFunction(data) {
this.assertEquals(add(data["val1"], data["val2"]), data["expected"]);
}

// This will not be ran as a test as it's marked as a provider
testAddFunctionProvider() {
return [
// This will fail
{"val1": 1, "val2": 2, "expected": 5},
{"val1": 2, "val2": 2, "expected": 4},
{"val1": 3, "val2": 2, "expected": 5},
{"val1": 4, "val2": 2, "expected": 6},
{"val1": 5, "val2": 2, "expected": 7},
// This will fail
{"val1": 6, "val2": 2, "expected": 9},
];
}
}

Test().run();
```

Output:
```
file.du
testAddFunction()
Line: 17 - Failure: 3 is not equal to 5.
```

## Assertions
### assertEquals(value, value)

Expand Down
1 change: 1 addition & 0 deletions src/optionals/http/http-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
" init() {}\n" \
"\n" \
" json() {\n" \
" // Import needs to be local to ensure HTTP is defined correctly\n" \
" import JSON;\n" \
"\n" \
" return JSON.parse(this.content);\n" \
Expand Down
21 changes: 12 additions & 9 deletions src/optionals/unittest/unittest-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
" var METHOD_NAME_PADDING = ' ';\n" \
" var RESULTS_PADDING = ' ';\n" \
" var ASSERTION_PADDING = ' ';\n" \
" var results = {\n" \
" 'passed': 0,\n" \
" 'failed': 0,\n" \
" 'skipped': 0\n" \
" };\n" \
"\n" \
" init(var onlyFailures = false, var exitOnFailure = false) {}\n" \
" var forceOnlyFailures = false;\n" \
" var forceExitOnFailure = false;\n" \
"\n" \
" init(var onlyFailures = false, var exitOnFailure = false) {\n" \
" this.results = {\n" \
" 'passed': 0,\n" \
" 'failed': 0,\n" \
" 'skipped': 0\n" \
" };\n" \
" }\n" \
"\n" \
" filterMethods() {\n" \
" return this.methods().filter(def (method) => {\n" \
Expand Down Expand Up @@ -80,15 +83,15 @@
" if (success) {\n" \
" this.results['passed'] += 1;\n" \
"\n" \
" if (not this.onlyFailures) {\n" \
" if (not (this.onlyFailures or this.forceOnlyFailures)) {\n" \
" print('{}Success.'.format(UnitTest.ASSERTION_PADDING));\n" \
" }\n" \
" } else {\n" \
" this.results['failed'] += 1;\n" \
"\n" \
" print('{}Line: {} - {}'.format(UnitTest.ASSERTION_PADDING, Inspect.getLine(2), errorMsg));\n" \
"\n" \
" if (this.exitOnFailure) {\n" \
" if (this.exitOnFailure or this.forceExitOnFailure) {\n" \
" System.exit(1);\n" \
" }\n" \
" }\n" \
Expand Down
21 changes: 12 additions & 9 deletions src/optionals/unittest/unittest.du
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ abstract class UnitTest {
var METHOD_NAME_PADDING = ' ';
var RESULTS_PADDING = ' ';
var ASSERTION_PADDING = ' ';
var results = {
'passed': 0,
'failed': 0,
'skipped': 0
};

init(var onlyFailures = false, var exitOnFailure = false) {}
var forceOnlyFailures = false;
var forceExitOnFailure = false;

init(var onlyFailures = false, var exitOnFailure = false) {
this.results = {
'passed': 0,
'failed': 0,
'skipped': 0
};
}

filterMethods() {
return this.methods().filter(def (method) => {
Expand Down Expand Up @@ -80,15 +83,15 @@ abstract class UnitTest {
if (success) {
this.results['passed'] += 1;

if (not this.onlyFailures) {
if (not (this.onlyFailures or this.forceOnlyFailures)) {
print('{}Success.'.format(UnitTest.ASSERTION_PADDING));
}
} else {
this.results['failed'] += 1;

print('{}Line: {} - {}'.format(UnitTest.ASSERTION_PADDING, Inspect.getLine(2), errorMsg));

if (this.exitOnFailure) {
if (this.exitOnFailure or this.forceExitOnFailure) {
System.exit(1);
}
}
Expand Down