---
title: How to Run Integration Tests Using Docker Compose and .NET 5
description: Learn how to quickly configure and run a production-ready integration test using .NET 5 containers with docker-compose and GitHub actions.
image: https://blog.avenuecode.com/hubfs/pexels-pixabay-262488.jpg
---

[AvenueCode.com](https://avenuecode.com) [News](https://avenuecode.com/news) [Contact](https://avenuecode.com/contact)

[![Avenue Code Snippets Logo](https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/AC-Snippets---Black.png)](https://blog.avenuecode.com/?hsLang=en-us) *menu*

- Technology
  
  [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us) [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us) [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us) [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us) [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
  
  [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us) [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us) [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us) [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

- [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us)
- [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us)
- [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us)
- [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us)
- [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us)
- [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us)
- [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us)
- [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

# [How to Run Integration Tests Using Docker Compose and .NET 5](https://blog.avenuecode.com/how-to-run-integration-tests-using-docker-compose-and-.net-5)

- [Tweet](https://twitter.com/share)

*perm\_identity* [Alvaro Kramer](https://blog.avenuecode.com/how-to-run-integration-tests-using-docker-compose-and-.net-5/author/alvaro-kramer?hsLang=en-us)

*schedule* 2/3/21 2:00 PM

Writing automated tests is an essential part of the development process, but it's not unusual to find codebases with little or no test coverage. Compared to unit tests, integration tests bring even more challenges since they get the same results running in different OSs (different developer machines and CI pipelines), but they are necessary in evaluating the effectiveness of different software modules when they are interconnected.

##### What Are Integration Tests?

In this article, we are considering integration tests as a specialized type of automated test where software functionalities are combined and tested as a group. Integration tests (also called service tests) are at the middle of the test pyramid, and their purpose is to verify that separately developed modules work together properly. Unlike unit tests, integration tests can depend on databases and external APIs.

##### Integration Tests in .NET 5

The .NET 5 framework makes some great possibilities available for integration tests. One of these is the [EF Core In-Memory,](https://docs.microsoft.com/en-us/ef/core/providers/in-memory/?tabs=dotnet-core-cli) which is a very good option and fits in different scenarios, but it doesn't exactly replicate a database behavior. So if you're searching for an approach to use with different ORMs ([Entity Framework](https://docs.microsoft.com/en-us/ef/), [NHibernate](https://nhibernate.info/), [Dapper](https://stackexchange.github.io/Dapper/), etc.) or an approach that will bring you other possibilities (like running an environment as code), we think `docker-compose` with integration tests will be a very useful approach for you.

##### Using Docker and Docker-Compose

We chose a TimeSheet application as an example for this article. In this application, the employee of a fictional company will submit hours he/she worked throughout the week. The API layers are separated by folders, and there are only two `.csproj` files, one for the API, and the other for the tests. The [code](https://github.com/luizhlelis/dotnet-docker-integration-test/) is hosted on GitHub.

The `docker-compose` starts three containers: `timesheets-api`, `sql-server-database`, and `integration-tests`. The first and third ones are the same image (`dotnet/sdk:5.0`), `timesheets-api` to run the API inside the container, and `integration-tests` to set up and run tests. Both services have a configuration map to host [volumes](https://docs.docker.com/storage/volumes/) to be used inside the containers. This setting allows the container to use the source code implemented on the host machine.

![Screenshot (106)](https://blog.avenuecode.com/hs-fs/hubfs/Screenshot%20(106).png?width=1130&name=Screenshot%20(106).png)

The `timesheets-api` container is started with a `dotnet run`. The `integration-tests` has a more complicated command. The script `wait-for-it.sh` keeps checking if the provided port of the service (sql-server-database:1433) is available. Once it gets the right response, the `dotnet test` executes the tests.

The `sql-server-database`, as the name says, works as the containerized database to run the API pointing to the database (environment as code), or as a test database. The image is an SQL Server (`mssql/server`). Also, we need to configure the database service in the compose file to expose the `1433` port and bind it in the host's same port.

![Screenshot (105)](https://blog.avenuecode.com/hs-fs/hubfs/Screenshot%20(105).png?width=1085&name=Screenshot%20(105).png)

The database connection string is created on the `appsettings.json`:

![Screenshot (107)](https://blog.avenuecode.com/hs-fs/hubfs/Screenshot%20(107).png?width=1116&name=Screenshot%20(107).png)

As you can see, our application will point to a localhost database when running. It's important to override the connection string to work on [containers network](https://docs.docker.com/network/), which was done in the environment step of `integration-tests` and `timesheets-api`. We chose an approach that uses only one `appsettings.json` and many overrides. This approach helps developers to avoid spreading `appsettings.json` files along with the code (which we consider a code smell), and even if the developer chooses to use different `.env` files in the `docker-compose` environment step, these will be centralized in the same place in the folder's hierarchy.

To run the project locally (if you want to debug in your favorite IDE, for example), you only need to use the SQL Server: `docker-compose up -d sql-server-database`. Otherwise, if you want to use an environment as code approach, run in your bash: `docker-compose up -d timesheets-api`; that command will initialize the database (SqlServer) and the API on docker. Finally, if you want to run the integration tests, run this command: `docker-compose up integration-tests`. That command will run the .NET 5.0 and SQL Server containers and then execute the tests.

##### Setting Up the CI

We use the Github Actions to run the tests when any PR or modification at the `master` branch is done.

To set up the workflow, create a new file in the `.github/workflows` named `integration-tests.yml`

````This action starts the integration-tests container with all dependencies. The argument `--exit-code-from` uses the exit code of the selected service container as the result of the action.

When the file is committed to the GitHub repository, the action will be available to be triggered on every commit to the master branch or when a PR to the master branch is created.

To see the workflow results, click **Actions**.

![Screenshot (105)-1](https://blog.avenuecode.com/hs-fs/hubfs/Screenshot%20(105)-1.png?width=1120&name=Screenshot%20(105)-1.png)

It is also possible to expand the logs to see the details. If a test fails, we can take a look at the results to troubleshoot.

![Screenshot (106)-1](https://blog.avenuecode.com/hs-fs/hubfs/Screenshot%20(106)-1.png?width=1139&name=Screenshot%20(106)-1.png)

##### Conclusion

Running integration tests with `docker-compose` is a very helpful option to replace in-memory databases. The article showed how to quickly and easily configure a production-ready integration test approach and demonstrated how to run integration tests using the new .NET 5 containers with `docker-compose` and `github actions` for a Continuous Integration. It's helpful to test the application from a request to a database persistence, and this approach can also be applied using technologies other than .NET and `github actions`.

Check out more about integration tests in ASP.NET Core [here](https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-5.0), and tell us how this approach works for you in the comments below!

\*This post was co-written by [Luiz Lelis](https://github.com/luizhlelis), [Rafael Miranda](https://github.com/rafaelpadovezi), and [Stefano Bretas.](https://github.com/sbpietro)

##### References

[IntegrationTest by Martin Fowler](https://martinfowler.com/bliki/IntegrationTest.html)

[Engenharia de Software Moderna](https://engsoftmoderna.info/)

- [TestPyramid by Martin Fowler](https://martinfowler.com/bliki/TestPyramid.html)

``

---

### Author

# Alvaro Kramer

 Alvaro Kramer is a .NET Engineer at Avenue Code. He graduated with a degree in Information Systems from PUCRS. His professional experience is focused on .NET technology. Outside work, he is a traveler and a sports, beach and craft beer fan.

---

### Related Posts

### How to Build Unit Tests Using Jest

[READ MORE](https://blog.avenuecode.com/how-to-build-unit-tests-using-jest?hsLang=en-us)

### How Heuristics Can Improve Your Tests

[READ MORE](https://blog.avenuecode.com/how-heuristics-can-improve-your-tests?hsLang=en-us)

### How to Use WireMock for Integration Testing

[READ MORE](https://blog.avenuecode.com/how-to-use-wiremock-for-integration-testing?hsLang=en-us)

### The Best Way to Use Request Validation in Laravel REST API

[READ MORE](https://blog.avenuecode.com/the-best-way-to-use-request-validation-in-laravel-rest-api?hsLang=en-us)

### Leave a Comment!

### Avenue Code Social

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Blog/facebook.png?t=1486470796564)](https://www.facebook.com/avenuecode)

[![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Blog/twitter.png?t=1486470796842)](https://twitter.com/AvenueCode)

[![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png?t=1486470796556)](https://www.linkedin.com/company/avenuecode/)

### Newsletter

Want to stay on top of all tips and news from Avenue Code?

### Popular Snippets

![Avenue Code-primary versions_logo white avenue code endorsement 2](https://blog.avenuecode.com/hs-fs/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png?width=1180&name=Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png "Avenue Code-primary versions_logo white avenue code endorsement 2")

### About Us

- [Who We Are](https://www.avenuecode.com/who-we-are)
- [What We Do](https://www.avenuecode.com/what-we-do)
- [Portfolio](https://www.avenuecode.com/portfolio)
- [Partners](https://www.avenuecode.com/partners)
- [News](https://www.avenuecode.com/news)
- [Events](https://www.avenuecode.com/events)
- [Blog](https://blog.avenuecode.com/)
- [Contact](https://www.avenuecode.com/contact)

### Our Offices

San Francisco

[+1 415 766 4178](tel:+553125161448) [ac.inquiries@avenuecode.com](mailto:brazil.info@avenuecode.com)

Belo Horizonte

[+55 31 2516 1448](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

São Paulo

[+55 11 3205 3232](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

### We're Hiring!

- [Belo Horizonte](https://www.avenuecode.com/who-we-are)
- [New York](https://www.avenuecode.com/what-we-do)
- [San Francisco](https://www.avenuecode.com/portfolio)
- [São Paulo](https://www.avenuecode.com/partners)

---

©2015 - 2017 Avenue Code

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Icons/facebook-2.png)](https://www.facebook.com/avenuecode) [![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Icons/twitter-2.png)](https://twitter.com/AvenueCode) [![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Icons/linkedin-2.png)](https://www.linkedin.com/company/avenue-code) [![Glassdoor Icon](https://blog.avenuecode.com/hubfs/Images/Icons/glassdoor-icon-1.png)](https://www.glassdoor.com/Overview/Working-at-Avenue-Code-EI_IE456173.11,22.htm) [![YouTube Icon](https://blog.avenuecode.com/hubfs/Images/Icons/youtube-2.png)](https://www.youtube.com/user/AvenueCodePlay)

Please enable JavaScript to view the [comments powered by Disqus.](http://disqus.com/?ref_noscript)

© 2026 Avenue Code

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Alvaro Kramer",
    "url" : "https://blog.avenuecode.com/author/alvaro-kramer"
  },
  "dateModified" : "2022-10-12T19:44:09.501Z",
  "datePublished" : "2021-02-03T17:00:00.000Z",
  "headline" : "How to Run Integration Tests Using Docker Compose and .NET 5",
  "image" : [ "https://blog.avenuecode.com/hubfs/pexels-pixabay-262488.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.avenuecode.com/how-to-run-integration-tests-using-docker-compose-and-.net-5",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_LOGO%20HORIZONTAL%20group%201-7.png"
    },
    "name" : "Avenue Code"
  }
}
```