Automated Integration Testing over Gitlab CI/CD for Dotnet Core via Testcontainers

Part 1 : Integration Testing with Dotnet Testcontainers

Mustafa Onur Aydın
Trendyol Tech
4 min readAug 2, 2020

--

I love Java there is no doubt. In my career of engineering i used so many different programming languages, tools even microprocessors. After passed 7 year, almost all years i was mostly a part of some Java based projects as a backend developer. The passed year in my journey with Trendyol, i had chance to discover Golang, improve my frontend skills with ReactJS when i was working as a Java developer. In last 3 month, i catch a chance to discover Dotnet Core via C#. I’m grateful to Trendyol for giving chance to us trying different technologies.

What you can find in this article series?

I will try to explain, how to apply automated testcontainers approach with dotnet-testcontainers like in Java testcontainers. This article will be two part. First part will be around, how to use docker containers for integration tests for Dotnet Core. Second part will be around how to automate testing over Gitlab CI/CD pipeline.

What you can not find in this article?

In this article, i will not compare programming languages tools etc like “Java is better than C#” :) The main aim of article explaining of applying test practices. The best programming languages/tools is not certain and depends your human resources. The truth is practices are same for different platforms.

Part 1 : Integration Testing with Dotnet Testcontainers

Why we need integration tests? You can find tons of article about this question. The shortest answer is being sure that your development is compatible with your production environment.

In last three month, i was working in Charge project in Trendyol. The project responsibility is to manage cancel and claim processes. To achieve success, we need to be sure that our Charge micro service is covering all edge and boundary cases. In this scenario end to end tests very useful.

There is a pretty good solution for java which is called testcontainers. I decided to apply this practice over dotnet-core. After my research i saw some alternative ways to writing dockerize test containers over dotnet.
There is a pretty good solution from Andre Hofmeister which is called, dotnet-testcontainers . This library supports lots of functionality for creating docker containers for the integration tests.
Dotnet-testcontainers has folowing pre-configured solutions;

  • CouchDB (couchdb:2.3.1)
  • MsSql (server:2017-CU14-ubuntu)
  • MySql (mysql:8.0.18)
  • PostgreSql (postgres:11.5)
  • Redis (redis:5.0.6)
  • RabbitMQ (rabbitmq:3.7.21)

In our micro service we need Couchbase as a test container. I developed a pre-configured Couchbase container over dotnet-testcontainers. It took time when i was developed. Also there was challenges to use Couchbase as a testcontainer with minimal resources.

I love open source ❤️ . And also Trendyol advices to us contributing open source community too.

Andre’s dotnet-testcontainers library is an open source project. I decided to contribute dotnet-testcontainers and i contacted with Andre. He is really kind person. He accept our contributing request and we discussed how we can improve dotnet-testcontainers with new features.

After all, I contributed with Couchbase test container to dotnet-testcontainer ! 🎉 🎉 🎉 🍾 🍾 🍾

You can use my Couchbase test container solution after 1.4.0-beta.20200723.17 version of dotnet-testcontainers. Thank you Andre for this amazing library!

Here is a sample, how to use dotnet-testcontainers with pre-configured Couchbase container.

You can create a Couchbase docker container easily like above. This code, creating a cluster with “Administrator” username and “password” password. You can define initial bucket like above “Charge”parameter. Now you can start Couchbase test container with following code;

await couchbase.StartAsync();

After that, you can write your tests which is using Couchbase repository.

If you need more bucket, you can create like following;

await couchbase.CreateBucket("Customer");

This function creating bucket with 128mb bucket memory. If you need to customize than you can create bucket like following;

await couchbaseTestcontainer.CreateBucket("Customer", 256);

Now we are able to write integration tests for Couchbase.

Here is a sample application over gitlab which i wrote;
https://gitlab.com/dotnet-testcontainers/couchbase-testcontainers-sample

Lets check;
There is a provider class for using Couchbase with dependency injection.

Now we can develop a repository like following;

Now we can write our tests for DocumentsRepository. Unfortunately, over dotnet core we need to do something more for use dependency injection over integration tests. Following ItContext class is creating and starting CouchbaseTestcontainer and initializing dependency injection context for tests.

After that, we should define a collection for use that ItContext as a fixture;

After all we are ready to write tests like following.

After that [Collection(“ItContext”)] definition, we are able to use dependency injection context.
You can access the sample project from following gitlab repository;
https://gitlab.com/dotnet-testcontainers/couchbase-testcontainers-sample

Conclusion

This article will help to understand writing integration tests for dotnet core via dotnet-testcontainers. The second part will focus more automatize integration tests over Gitlab CI/CD pipelines with docker near docker. The right known wrong is docker in docker is same docker near docker. Actually is not. Please keep follow for more details.

Thanks for reading this article! Do not hesitate to make criticism with comments.

--

--