Member-only story

Make your C# Automated Tests More Clear with xUnit Assertions

Alex Siminiuc
3 min readApr 23, 2021

--

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
Product[] products = api.GetProductsFor(category);

foreach (var p in products)
{
Assert.False(String.IsNullOrEmpty(p.name) ,
“name is null or empty”);

--

--

Alex Siminiuc
Alex Siminiuc

No responses yet