Unit testing for Microsoft Azure Service Fabric


I investigate some strategy for unit testing for Service Fabric. Service Fabric is the PaaS environment for Microservices. It is very innovative service. I really love it. However, it is difficult for unit testing.
I'm still not sure which is the best practice for Service Fabric, however, I'd like to share what I leaned.

2016-09-02

Unit Testing Strategies

We have several strategy for unit testing for Service Fabric.

a. Mock object

Service Fabric Web Reference Application contains the Unit Testing example for Service Fabric. However, it looks not cool. Because it has a lot of Mock objects code without using a Mock object framework. If the API were changed, we would refactor it. Also, the production code depends on Mock project. However, it is enable to write unit tests on Service Fabric quite easily.

b. Using A local cluster

Haishi Bai mentioned this strategy on the book Programming Microsoft Azure Service Fabric.
If you have a local cluster on your machine, you can use this strategy. It makes very easy to write a unit test for Service Fabric.

Sample Source(GitHub): GameTests.cs

However, if you want to use a hosted build of the Visual Studio Team Services, it won't work. Instead, you need to create a build machine by yourself. Also, building a build machine for Service Fabric is little bit complex. Also, you can't use this strategy for remote cluster on the current version(5.1.150). It might take a long time for executing the tests for production.

c. Mock Object Framework

You can use a mock object framework. Moq looks cool. However, when I try Moq for Service Fabric, It will require a lot of "SetUps" before starting a unit test. It is quite painful. StatefulService requires a lot of Mocking. StateManager, ReliableDictionary, Transaction,  and so on.

You can see the reference applications of the Mocks project.

Current Solution

My current solution is mixed one between a. and c. I'm using Mocks for easy to write a unit test. However, I remove the dependency from production to Mocks project. The reference application write a mock for a service on the Mocks project. I move it to the test projects.

Also setting up a Mock for ICodePackageActivationContext for each unit testing class is painful. So I use Moq in this part.

You can refer sample code for this solution on this commit;

Sample Source (GitHub):

Stateful Service Testing:  ServiceFabricIoTSample/ResilientIoT/IoTPartitionMapper.Tests/IoTPartitionMapperServiceTest.cs

Actor Testing: ServiceFabricIoTSample/ResilientIoT/SensorActor.Test/SensorActorTest.cs

Conclusion

Obviously, this is not the best solution. I hope in the near future, the Service Fabric support unit testing mechanizm by them self like Chaos-Testing-Buildin-Framework. I'm a big fan of the Service Fabric at the same time, I'm a DevOps guy. I need to automate everything. 🙂

Service Fabric change the architecture. When Haishi Bai worte the book, the Service Fabric didn't have the StateManager. However, StateManager's methods require async/await programming. That is why the sample program of this book is totally changed. The book contents is really helpful.

So I re-write a sample code of this book on my GitHub When I tried the github, I didn't know about C# concurrent programming style. So sometimes, I wrote some bad code. However, you can see the solution how to convert the book code into the latest ServiceFabirc code. I'm refactoring whole code into suitable ones.

If you have a good solution for the unit testing for the Service Fabric. Please let me know.


Comments (3)

  1. I update this post for adding actor testing

Skip to main content