Test driven development and X++

Test driven development (TDD) is a great way to work with unit testing in D365FO development. There are multiple benefits working with and introducing automatic tests for your code base.
  • You’ll code correctly faster, 
  • Good tests make good documentation
  • You won’t forget to write the tests
  • You’ll write more meaningful tests when you test before code
  • It makes introducing changes later in ALM easier
  • It pushes you to write cleaner code, smaller pieces of code to make testing easier. 
The principles of TDD are as follows. 
  • You are not allowed to write any production code unless it is to make a failing unit test pass.
  • You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  • You are not allowed to write any more production code than is sufficient to pass the one failing unit test. 
If followed they will ensure that the developer is either working on writing test code or business logic.

In X++ it's easy to follow these principles by coding against an interface in the business model and testing against it in the test model. This way reduce the code that is required in the business model before writing the test. 

To get started with automatic tests create a test model related to your business model. If you business logic model is called for example pekFeature the related test model would be named pekFeatureTests following the pattern of <business logic model name>Tests. Make sure that your test model references TestEssentials, this model contains the SysTestCase class that all tests need to extend.  

Writing a test is as simple as in below example. The pekCalculator class would reside in the business logic model and the related test class named Test like pekCalculatorTest would reside in the separate test model. 


Writing a test is as simple as below, start by extending SysTestCase.

Each test should be named test<description of test> and tagged with the attribute [SysTestCheckInTest] .

It's also recommended to divide each test into a Arrange, Act and Assert section to make it easier to read. Use the arrange section to setup the data and prerequisites for the test. The Act section to perform the action that is being tested and the Assert section to verify that the expected outcome was produced by the Act section. 

See my repository for examples here, https://github.com/ponekbladh/d365foblog

Comments

Post a Comment

Popular posts from this blog

How to disable auto enabled flight key using KillSwitch

Technical intro to Feature management in D365FO

Continuous integration and deployment Power platform FinOps tweaking