ASP.NET Core 2.0 Integration Testing with Identity Server — In Docker

Stuart Tottle
2 min readJan 12, 2018

--

TL;DR This is the 2nd part to this post. We will now run an integration test against an API endpoint that requires authentication in docker containers. We will

  • Setup environment variables
  • Create dockerfiles for Identity Server and Tests
  • Create a bash script to run tests in containers

See the final code here (on docker branch)

Update API

In the MyApiServer project, update the JWT Bearer Options method to pull in the configuration for the Identity Server from environment variables

Update Identity Server

In the MyIdentityServer project, add the dockerfile to

  • Restore Nuget packages
  • Run Identity Server on port 8002

Update Tests

In the MyApiServerTests project, update the constructor in UnitTest1 to pull in the configuration for the Identity Server

And add the dockerfile to

  • Restore Nuget packages
  • Set Environment Variables
  • Run the tests

Add dockerignore

Add .dockerignore in the root of the project (same level as sln file) to exclude folders when building containers

Add bash script to run tests

Create tests.sh in the root of the project to build and run the images to execute the tests

Now run ./tests.sh from a terminal and you should see two passing tests.

Notes:

  • You might need to make the bash script executable chmod +x ./tests.sh
  • Bash scripts can be run in Powershell on Windows

Conclusion

We now have our tests running in containers, but we could still do a few more things which go beyond this post

  • Create a docker network in which you can run the containers
  • Use docker-compose instead of a bash script

--

--

Responses (2)