XUnit – Part 6: Testing The Database with xUnit Custom Attributes

In this Often we need to test our database code. There are a lot of ways to do that, but I think  the cleanest way is to create a custom attribute for it. What we want to achieve is to create a custom attribute. In this  attribute we pass the information need to connect to the database. In another attribute we pass the query that we want to run against that database and the result we expect. So in this post I’m going to go through the process of testing the database with xUnit for Sql Server and Oledb databases.

Custom Attribute for Testing SQL Server Database

In this section we’re going to write an attribute that help us to test our sql server database. Our final attribute will be used like this.

The fist step is to create the attribute and receiving all the arguments into our attribute.

There are two variation in using this attribute. First is connecting to our database using a trusted connection. The other type receive user name and password. We also inherited from OleDbDataAttribute which is going to do the heavy lifting for us. Until this point, we didn’t write the most important part of our code that query the database. I’m going to explain how to do that in subsequent sections.

Custom Attribute for Testing OleDb Database

The this section we’ll see how we can write a custom attribute for OleDb so it can be used like this.

For OleDb because we pass the connection string directly, so there are no variation. Also note that this attribute is inherited by our previous SqlServerDataAttribute. This attribute is the one that eventually handle the query to the database.

Note that this attribute inherited from DataAdapterDataAttribute, in the next section I’m going to explain how that attribute is used to query the database. Another inportant point is how we use the OleDbDataAdapter, in the next section we’re going to use this OleDbDataAdapter to retrieve information.

DataAdapterDataAttribute for Querying the Database

The last step is to implement a class the retrieve data for us and connect it with the testing framework. Let’s have a look at this class.

This class inherits from DataAttribute and contain the abstract method GetData. Here we override this method and query the data using DataAdapter and we created in previous step. But how to we hook this class into the xUnit framework? This is done in DataAdapterDataAttributeDiscoverer class. Note that DataAdapterDataAttribute class is using a DataDiscoverer attribute that point to this class.

The most important thing to point out here is EnableDiscoveryEnumeration, here’s how the xUnit framework describe this property.

Returns true if the data attribute wants to enumerate data during discovery. By default, this attribute assumes that data enumeration is too expensive to do during discovery, and therefore defaults to false.

Further Reading

Testing With Databases

Integration Testing with xUnit

xUnit Theory: Working With InlineData, MemberData, ClassData

Summary

In this post, we saw how we can create a custom attribute that can help us in testing the database with xUnit. We saw how we can do that for both Sql server and oledb. The if you need to code used as an example in this post, they can be found in this repository.

Share...
 

Hamid Mosalla

Hi, I'm Hamid Mosalla, I'm a software developer, indie cinema fan and a classical music aficionado. Here I write about my experiences mostly related to web development and .Net.

 

Leave a Reply

Your email address will not be published.