Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge support for fact based unit tests
- Loading branch information
Showing
10 changed files
with
217 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Fact based unit tests | ||
|
||
If you are familiar with [fact based unit tests][Microsoft Unit Testing] (as | ||
shown in Microsoft documentation) you can continue to use that approach with | ||
LibPQ UnitTest framework. Integrating existing fact based test suites will | ||
require some extra code, but the tests themselves need no modification. | ||
|
||
> **NOTE:** Authors believe that LibPQ [UnitTest] offers more features and has | ||
> more stable API than fact based tests offered by Microsoft. We recommend | ||
> writing new tests with native [UnitTest] approach and using fact based tests | ||
> only to integrate pre-existing test code | ||
## Writing fact based unit tests | ||
|
||
With fact based tests a **test case** is a fact generated by `Fact()` function | ||
that takes three arguments: test description, expected value, actual value. | ||
Such facts are grouped into **test suites** by packing them in a list. Test | ||
suites are executed with `Facts.Summarize()` function. | ||
|
||
LibPQ provides its own implementation of [Fact] and [Facts.Summarize] | ||
functions. Those functions have the same API as the ones described in | ||
Microsoft's article but they produce different output to enable compatibility | ||
with LibPQ UnitTest framework. | ||
|
||
To run fact based tests with LibPQ you need to import those functions into | ||
your test code. No other modifications are required. | ||
|
||
```javascript | ||
// ...beginning of the test code (skipped)... | ||
|
||
// Import LibPQ functions that enable fact based tests | ||
Fact = LibPQ("UnitTest.Fact"), | ||
Facts.Summarize = LibPQ("UnitTest.Facts.Summarize"), | ||
|
||
// Sample fact based test | ||
Fact("Values should be equal (using a let statement)", | ||
"Hello World", | ||
let | ||
a = "Hello World" | ||
in | ||
a | ||
), | ||
|
||
// ...the rest of the test code (skipped)... | ||
``` | ||
|
||
## Test discovery for fact based tests | ||
|
||
LibPQ UnitTest framework supports test discovery for fact based unit tests. To | ||
make your tests discoverable you need to: | ||
|
||
- Store the test suite (list of facts) in a separate LibPQ module | ||
- Add the following metadata to the test suite: `LibPQ.TestSuite = "Facts"` | ||
|
||
Such test will be discovered and executed automatically by | ||
[UnitTest.Discover]. See [sample test] for more information. | ||
|
||
## Converting existing test code for use with LibPQ | ||
|
||
The only thing you need to do to execute your existing fact based tests with | ||
LibPQ is to inject the LibPQ versions of [Fact] and [Facts.Summarize] | ||
functions into your test code. You can do that by adding the following import | ||
statements to your modules: | ||
|
||
```javascript | ||
Fact = LibPQ("UnitTest.Fact"), | ||
Facts.Summarize = LibPQ("UnitTest.Facts.Summarize"), | ||
``` | ||
|
||
After that you can consider making your test suite discoverable by LibPQ (as | ||
described previously in this article). | ||
|
||
[Microsoft Unit Testing]: https://docs.microsoft.com/en-us/power-query/handlingunittesting | ||
[UnitTest]: UnitTesting.md | ||
[Fact]: ../Modules/UnitTest.Fact.pq | ||
[Facts.Summarize]: ../Modules/UnitTest.Facts.Summarize.pq | ||
[UnitTest.Discover]: ../Modules/UnitTest.Discover.pq | ||
[sample test]: ../Modules/Tests.MicrosoftUnitTestDemo.pq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
Demo of LibPQ's compatibility with Microsoft's unit testing framework | ||
https://docs.microsoft.com/en-us/power-query/handlingunittesting | ||
**/ | ||
|
||
let | ||
Fact = LibPQ("UnitTest.Fact"), | ||
Facts.Summarize = LibPQ("UnitTest.Facts.Summarize"), | ||
|
||
UnitTesting.ReturnsABC = () => "ABC", | ||
UnitTesting.Returns123 = () => "123", | ||
UnitTesting.ReturnTableWithFiveRows = () => Table.Repeat(#table({"a"},{{1}}),5), | ||
|
||
facts = | ||
{ | ||
Fact("Check that this function returns 'ABC'", | ||
"ABC", | ||
UnitTesting.ReturnsABC() | ||
), | ||
Fact("Check that this function returns '123'", | ||
"123", | ||
UnitTesting.Returns123() | ||
), | ||
Fact("Result should contain 5 rows", | ||
5, | ||
Table.RowCount(UnitTesting.ReturnTableWithFiveRows()) | ||
), | ||
Fact("Values should be equal (using a let statement)", | ||
"Hello World", | ||
let | ||
a = "Hello World" | ||
in | ||
a | ||
) | ||
} | ||
in | ||
facts meta [LibPQ.TestSuite = "Facts"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
Helper function that packs arguments into a record. | ||
This is a part of compatibility layer between LibPQ and Microsoft's framework | ||
for unit testing in Power Query | ||
More information: https://docs.microsoft.com/en-us/power-query/handlingunittesting | ||
**/ | ||
|
||
(test_name as text, expected_value, actual_value) => | ||
[ | ||
name = test_name, | ||
expected = expected_value, | ||
actual = actual_value | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
Run tests represented as a list of facts. | ||
This is a part of compatibility layer between LibPQ and Microsoft's framework | ||
for unit testing in Power Query | ||
More information: https://docs.microsoft.com/en-us/power-query/handlingunittesting | ||
**/ | ||
|
||
(facts as list) => | ||
let | ||
UnitTest.Run = LibPQ("UnitTest.Run"), | ||
Assert = LibPQ("UnitTest.Assert"), | ||
Config = LibPQ("UnitTest.Constants"), | ||
|
||
Tests = List.Accumulate( | ||
facts, | ||
[], | ||
(collection, fact) => | ||
Record.AddField( | ||
collection, | ||
Config[Test.Prefix] & " - " & fact[name], | ||
Assert[Equal](fact[expected], fact[actual]) | ||
) | ||
), | ||
Result = UnitTest.Run(Tests) | ||
in | ||
Result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters