As your code base gets bigger, test automation becomes more and more important. This applies to any development platform, including Power Query / PowerBI. If you reuse your code and improve some low level function later, test automation allows you to make sure your changes did not break anything that depends on the part of code you've just modified.

As far as I know, there are no tools that allow us to perform automated testing of functions and queries written in Power Query M language. That's why I've built a simple unit testing framework into LibPQ.

LibPQ UnitTest framework

The UnitTest framework is modelled after the only other unit testing tool I have experience with: Python's unittest. It offers the following features:

Inner workings of the test framework are described in the documentation. This article will demonstrate how it works.

UnitTest demo

All modules described here are imported with LibPQ, so a basic familiarity with the library is assumed (readme, getting started).

Let's create a basic test suite and save it in the directory listed in LibPQPath:

/* DemoTests.pq - sample test suite */
[
    Assert = LibPQ("UnitTest.Assert"),
    testFirstTest = Assert[Equal](6*7, 42),
    testAlwaysFail = Assert[Equal]("foo", "bar")
] meta [LibPQ.TestSuite = 1]

The test suite is a record (note the square brackets surrounding the code) that contains:

The last line contains metadata that marks the test suite as such and allows test discovery tools to distinguish it from just another record.

Here is what UnitTest.Discover function will do when invoked:

Test results

In the screenshot above we invoke UnitTest.Discover with compact_output = false but when you'll have dozens of test cases you'll probably prefer default behavior (group test results by status).

More about UnitTest in LibPQ

If you liked the idea of unit testing M language code, check out the main UnitTest documentation and a more extensive test sample that makes use of subtests.