I published my testing setup with Umbraco to GitHub! For a while, I've been writing about testing and test-driven development in Umbraco. I've shared several examples to demonstrate how I test certain things and now I'm bundling everything together into a single working repository:
A working example of integration- and unittests with Umbraco. A demonstration of various concepts for testing your Umbraco website
Umbraco 16 automated testing setup
This project is a fully functioning setup for automated testing with Umbraco 16. You can use this project as a reference or starting point to get started with testing on your Umbraco website. The tests are set up with Test Driven Development (TDD) in mind.
Tools
The most important tools that are used in the automated tests are as follows:
Name |
Description |
xUnit v3 |
The testing framework. You can use any testing framework that you like though |
NSubstitute |
Library for mocking. Any mocking library will work. This project doesn't do extensive mocking, but for example IPublishedValueFallback is a mandatory parameter for any published content item, even if you don't actually use it. It's just convenient to insert a mock. |
Test Containers |
Automatically creates docker containers while running tests. It is used to create an empty SQL Server database that is automatically cleaned up after testing. |
…
This repository is meant to be used as a reference to get started with automated testing in an Umbraco project. Using practical examples, this repository demonstrates how you can cover various parts of your Umbraco application with automated tests.
What can I find in the repository?
The repository contains three projects:
- A small example website
- A unit testing project
- An integration testing project
In the unit testing project, you'll find examples that demonstrate how to write tests for code with IPublishedContent
. In the integration testing project, you'll find examples that demonstrate how to run your whole Umbraco website in-memory using a real SQL Server database. Each testing project comes with a README that explains how the various kinds of tests work and how to use the custom testing utilities.
"It doesn't cover my specific use-case!"
I intend to cover as much of Umbraco as possible, but I'm definitely not there yet! If there is a use-case that you wish to see covered, you can start a discussion! It would be amazing if we could turn this repository into the ultimate Umbraco testing reference! If you have any questions about automated testing in Umbraco, you may ask them in the discussions as well, but don't forget that there is a big Umbraco forum where many more people are able to help out! I occasionally look there as well.
"I have an idea how to test X in Umbraco!"
I welcome additions to this repository! Any examples on how to test with specific Umbraco features are welcome! Please start by making an "issue" in the issues section to suggest your addition. That allows me to understand what you're proposing and gives others a chance to share ideas as well. It also gives me a chance to point out if it has already been implemented before and I might spare you some time.
Can you publish it as a package?
I'm not going to make a package out of this project. I don't have time to maintain packages. Additionally, I find it important that everybody learns how testing in Umbraco really works. I think if this project turns into a package, its contents turn into "magic" and I don't stand for that. Furthermore, you don't have to wait until I update to the latest Umbraco versions, just to continue testing.
That being said: I don't mind accommodating a bit if somebody else wants to wrap this into a package for their own team / company / personal use. So if there is something I can do to make the project more "packable", don't hesitate to make a suggestion!
I'm still working on a few things
For example: I posted about testing your backoffice controllers in an integration test before, but it isn't included in the repo yet! I believe that there is a better way to test backoffice controllers nowadays and I'm still figuring that out.
That's all
I find great importance in automated testing and I see it as the best way to make great software. I hope that this project inspires all of you to get started as well if you haven't yet. Thank you for reading and I'll see you in my next blog!