Last week I wrote about bit fields and how they can be used to easily track boolean values. Unit testing is pretty important to me, because I make mistakes a lot. By writing tests, I can have a little bit of confidence in the code that I wrote.
Bit Fields
Knowing how bit fields work, there are a lot of different combinations that are valid and produce different results. Ideally I want to test every combination of the flags to make sure that the correct bits are being set.
Say that we have the following (admittedly contrived) scenario:
In this case a Schedule is created where there are seven booleans defined to indicate if the schedule is to be executed on that given day.
XUnit
For testing this, we want to be able to provide a DayOfWeek value to a schedule and verify that the correct booleans are set. If you like maths, you know that the number of combinations here is 2^7 – 1, or 127… 128 if you include 0. That’s a lot of cases to write by hand, so why not use some kind of reflection magic to do it?
Doing this gives XUnit all of the data it needs to run the tests. This boosts your unit test count, which makes you look like some kind of programming rock star, and makes sure that all of your stuff is working properly. So… uh… rock on! … I guess!