DEV Community

Dennis
Dennis

Posted on

Umbraco testing examples now also for Umbraco 17

I updated my testing examples repository to the latest Umbraco version.

GitHub logo D-Inventor / automated-testing-in-umbraco

A working example of integration- and unittests with Umbraco. A demonstration of various concepts for testing your Umbraco website

Umbraco 17 automated testing setup

This project is a fully functioning setup for automated testing with Umbraco 17. 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.

Though not much has changed, here are the most notable differences compared to the Umbraco 16 version

Database initialization

The SqlServerDatabase resource no longer creates a second database inside the SqlServer test container. While Umbraco 16 would happily create a new database on boot if it didn't exist yet, Umbraco 17 does not and will throw connection errors.

For your regular use of Umbraco, this change makes no significant difference. If you also use EF Core, you may need to pay some extra attention here!

A base URL for integration tests

The WebsiteFixture will now set a fixed base URL when creating a backoffice httpclient. If you need to use a different domain, this base URL can be adapted, but what is important is that the URL uses https protocol. Compared to Umbraco 16, Umbraco 17 requires https to backoffice endpoints by default and requests using http are rejected by default. That's at least what the error would indicate.

If you want to see all the changes, you can check out this commit:
https://github.com/D-Inventor/automated-testing-in-umbraco/commit/573ef96a155c390ba3d4383bf555f9ff5723b3b4

An additional note

I have given some attention to the example website under test in this repository. The homepage at least has a slightly more fancy template, but it's all static content. I haven't yet taken the time to really tie it all together, and I wanted to do so using Test Driven Development, which is the main focus of this example repository.

I noticed that the version of playwright for C# doesn't actually let you compare screenshots!? This was a mild surprise and disappointment, because that means I can't actually use snapshot testing to ensure my template doesn't accidentally change when I implement the logic. The Javascript version of playwright does seem to support it, so I guess for better end-to-end testing, we'd need to create an equivalent management API client and scenario builder. Oof!

That's all

that's all I wanted to share! Hope you check out the repository, give it a star and let me know if the testing examples have been helpful to you or not! Thank you for reading 😊

Top comments (0)