Automated testing is an integral part of any software development project. Developers should be building these tests while they work on the actual code.
A significant value of automated tests is that they can be run on every pull request. This can ensure that the tests are run in a consistent environment and not just on developer machines.
How can we do this using C#, PostgreSQL, and GitHub? Pretty easily, actually.
Let’s create a console project and a unit test project.
This C# project uses unit tests and PostgreSQL. I created a simple DB class and a very simple Contact model.
The second thing we must do is write our unit test. The unit test in this example directly uses the DB context class, which I would typically advise against. But for the sake of a simple blog post, I went with it.
Now we should be able to run our automated test locally using a local copy of PostgreSQL.
With the tests running locally, we now need to include those tests in our build process. We will create a build .yml file for GitHub (Actions) that performs a few PostgreSQL commands.
Begin by starting PostgreSQL. Then, add a new user and create a database.
Now we have the automated builds that we will run each time we commit code.
Where to go from here?
The first and most obvious place to go is getting those database connections out of the code. That is a bad practice. I left it that way to make for a more straightforward demo, but that needs to change before continuing.