1. Home
  2. Training Library
  3. .NET Core C# - Test Driven Development (TDD) using xUnit

GitHub Action Workflow for Automatic Builds on Push Events

Start course

Test-Driven Development (TDD) is a discipline that helps to create error-free, quality code, and drives productivity by reducing and eliminating errors as well as providing many other benefits.

This entry-level training course is designed to bring you quickly up to speed with the basic features and processes involved in performing C# based test-driven development using xUnit. It covers the key principles and practices of test-driven development, which you can then utilize within your own software engineering projects.

In this course, you'll be guided through an end-to-end sample project in which we use test-driven development to build, test, and package a .NET Core 3.1 C# based library that will provide an API to convert Bitcoins into foreign currency.

If you have any feedback relating to this course, please contact us at support@cloudacademy.com.

Learning Objectives

  • Understand the fundamentals of TDD and xUnit
  • Use TDD to build, test, and package a .NET Core 3.1 C# based library
  • Perform TDD by performing Red-Green-Refactor iterations
  • Learn how to use TDD to build, test, and package a .NET Core 3.1 C# based library
  • Learn how to create unit tests using the xUnit testing framework

Intended Audience

  • Software developers interested in using TDD to build .NET Core applications using C#


To get the most out of this course, you should have a basic understanding of .NET Core and the C# programming language, as well as software development and the software development life cycle (SDLC).



Okay, welcome back. So in the previous demonstrations, we used test-driven development to build our Bitcoin converter library. Our Bitcoin converter library is now functional and all our unit tests are passing. So that's a great outcome. What I wanna do in this particular demonstration is show you how you can use GitHub to version control our source code and also perform CICD functions using GitHub actions, which will allow us to do automated builds anytime we push our code up in to GitHub.

Okay, let's keep busy. Okay, so within GitHub, I'm going to click on the new repository button and create a new repository called, demo-csharp-test driven development. Copy this into the description, I'll make it public, and we'll click the create repository button. So if you're following along, create your own repository in your own GitHub account. I'll then copy these commands here, jump back into my integrated terminal. And making sure that I'm in the Bitcoin converter directory which contains the following files and photos.

I'll then run those commands and this will require me to authenticate. And authentication is successful, and the upload of just the read me file for now has been done. Okay, so now if I do a git status, I can see that I've got these untracked files. So before I add them, what I wanna do is on the Bitcoin converter folder, I wanna add a new file and I wanna add ".git, ignore" which is a special file used by git to exclude files that are not part of the actual source code.

So these are your build files. So in .Net, it's your bin directory, your object directory, et cetera. So we don't need those in our GitHub repository. So for our ".git ignore" file I'm going to paste the following. You can copy this out of the repo that we began the whole set of demonstrations with. So I'll save that. I'll now do a Git status again. Looks good and I'll do a "git add." to add everything. And so this shows us that we've just got the files that make up our solution, which is great.

What I'll do now is I'll do a "commit-m" for message, initial upload. And then finally, I'll push it up into our repository. And there we go. So, jumping back into our repository, we've got our initial load, so that's great. So we've got all of our implementation and unit tests code into our GitHub repository.

Now, the focus of this particular demonstration is to now show you how to use, GitHub actions to actually do automated build and testing for us. If we click on the actions menu item, we can set up a GitHub action. So there's a bunch of templates which you can pick from or we can set up a workflow ourself.

So I'll take this option. I'll rename it to the .net-core.yml, And then I'll just delete everything that's here. And instead paste my envision like so. I'll then commit it. I'll jump back into our repo at the project level. And we can see that our new workflow has been established. Even better we can see that it's triggered the actual workflow as per this visual status here.

So if you click back into actions, and we click on the workflow item here, we click on build, we can actually see the build steps. And we can see that this build has completed successfully and how we can leverage GitHub actions to actually take on the role of being a CICD service for us.

Okay, let's now go back and update our workflow so that we can produce an actual artifact. So this will be our Bitcoin library DLL. So I'll jump back and this time I'll do a git pull. So now I've got access to our ".net core, GitHub action workflow. And then I'll simply add the following action to the bottom.

So in this case, we're using this custom action which allows us to upload any artifacts that are produced during the build phase. So in this case, because we're building this named DLL located here, we'll upload it as an artifact. So I'll save it, I'll then do a commit and push. And again, this will trigger the action.

So let's go and watch it. So click on actions. We can see our new workflow is in progress. You can navigate into it, click on the build and we can actually watch the build as it happens. So the second time it runs it should be quicker because it doesn't have to download the docker containers that are used in the background. That's simply cached. And also the in-store dependencies are also cached.

So the compilation is completed. The unit is now being run. We're now uploading the artifact. And the complete workflow is now completed successfully. Okay, if we navigate back, we can see that we've actually produced our, ".net core 3.1 Bitcoin converted.code.DLL artifact." This is very cool. And we can actually download the spec to our local Workstation.

Okay, then now complete this demonstration and watch I showed you how to use GitHub to do version control of our source code. As well as using GitHub actions to act as a CICD service for us, to build and test our source code, with the end result of an artifact being produced.

Okay, go ahead and close this demonstration. I'll see you shortly in the next one.

About the Author
Jeremy Cook
Content Lead Architect
Learning Paths

Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, GCP, Azure), Security, Kubernetes, and Machine Learning.

Jeremy holds professional certifications for AWS, GCP, and Kubernetes.