Authoring Azure DevOps Task — Testing Strategy (3)
You can find a lot of official tasks here.
However, you also find a lot of tasks that don’t have unit testing. I want to introduce how to write Azure DevOps Task unit test.
Unit Testing Strategies
For writing unit tests for your project, I find two types of your Azure DevOps custom task.
- External Command Execution
Custom Task that executes external commands - Service Interaction
Custom Task that uses Client Libraries or REST API
For External Command Execution, you can use a built-in unit testing framework. It helps you to mock and validate if the command is property executed with expected parameters.
However, if you want to validate the external call using a client library (e.g., call GitHub REST API), you need to write mock by your self.
Build-in unit testing framework
Let’s see the sample of this. The azure-pipelines-task-lib project recommends us to read XCode task as an example.
All we need is write a unit test and mock file.
Unit Test
You run the MockTestRunner with Mock configuration file. Then assert if the command is executed as you expected.
Mock configuration file
You need to configure the mock file to set
* Input of the Task
* Environment Variables
* Expected Return Value of a function
* Expected Result of a command execution
The build-in framework is an excellent way to test the “execute an external command” type custom task.
You can find whole sample here.
If you want to know the simpler one, my task might be the one.
Mock by your self
However, sometimes, the build-in framework doesn’t cover some testing scenario. For example, When I wrote the CreateCommentTask, that creates a Pull Request Comment, it requires to call Azure DevOps SDK. We can mock the library using the built-in framework. However, I can’t write proper assertion if a function of Azure DevOps SDK is property called or not. In this case, I had to mock by myself. I learned the technique from Rene.
Mock the azure-pipelines-task-lib
Using rewiremock and sinon, mock the azure-pipelines-task-lib. We can set the mock values to inputs and variables. If you need an additional mock method, please add it. You need to write an import statement of the target class that you want to mock the azure-pipelines-task-lib. Don’t forget the disable the rewiremock after using it.
Then write a test. You can configure inputs and variables
Mock the target library
I wrote the target class with a constructor which enable us to inject mock object.
Then create a mock object.
You can find the example in this repo.