Make your C# Automated Tests More Clear with xUnit Assertions

What makes an automated test more clear?

By automated test, I understand any test that automates a test case so the test can be for the UI, for an API or anything else.

An automated test is clear if it is short and it is written from the user’s point of view.

An automated test is clear if it is written at a higher level of abstraction.

An automated test is clear if it is independent of other tests.

It is clear if it has a limited number of assertions.

And …….

It is more clear if the assertions are specific to the verification being done.

Let me explain this last item with a few examples.

The following few lines of code get a list of products from an api. They also asserts for each product that

  • the product name is not null or empty
  • the product description is not null or empty
  • the price is greater than 0

What makes this code difficult to understand is not the assertions but the foreach loop used to go through all products.

The same code can be written as follows:

Isn’t this more clear than before?

There is no need of a foreach loop which is a good change since the automated tests should not use C# statements.

The code is shorter.

And we use Assert.All which applies another assertion to each element of the array of products. It is clear from the first glance that Assert.All is applicable to all array’s items.

The next few lines use the same array of products and check that it is not empty and that it has exactly 1 element:

Both assertions use the Assert.True method. To understand what the assertion does, we have to look at the condition passed as parameter:

We have to do 2 things to understand the purpose of the assertion. Check the assertion method used (Assert.True) and then check the condition passed as parameter to the assertion method.

This can be simplified as follows:

In this case, both assertion methods get the same parameter, the products array. It is clear what the assertion does just by looking at the assertion method. Assert.NotEmpty checks that the array parameter is not empty. Assert.Single checks that the array parameter has a single value.

The last example checks that the products array has between 1 and 10 elements:

As before, to understand what the assertion does, we look first at the assert method (Assert.True) and then at the complex condition passed as parameter.

The code becomes simpler when using a different assert method:

The Assert.InRange method takes 3 parameters: the number to check, the lower bound of the interval and the upper bound.

There are many other specific assertions available in xUnit that can be used similarly for simplifying C# automated tests:

  • string asserts
  • range asserts
  • record asserts
  • set asserts
  • type asserts
  • exception asserts
  • null asserts
  • skip asserts

Learn more about them on this link.


If you do not use C# but Java (or other languages) and the unit testing framework does not have specific assertions like xUnit, you can extend the Assert class and implement your own custom assertions.

Blogs about Selenium and Java at

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store