-
Notifications
You must be signed in to change notification settings - Fork 0
What Should I Test? #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Many organizations use a certain percentage of code coverage to determine if they are "testing enough" with automated tests. As with many things in life, better has more value than more. Let's talk about how we find where we need to have automated tests. I use the following phrase to drive my logic. "Look for the predicates". In software development, a predicate is an expression that returns either true or false. The use of predicates enables the flow of the code to branch. Some predicates are very easy to see, while some are a little more difficult, but these are the places where we should focus our tests. |
2fc1d7d
to
c2e9158
Compare
[TestMethod] | ||
public void Special_AfterConstruction_ShouldReturnValue() | ||
{ | ||
var menu = new Menu(null); | ||
|
||
menu.DailySpecial.Should().NotBeNullOrEmpty(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public Menu(IMenuItemProvider menuItemProvider) | ||
{ | ||
_menuItemProvider = menuItemProvider; | ||
DailySpecial = MenuUtilities.GetDailySpecial(DateTime.Now.DayOfWeek); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test we've written is actually testing line 15 in the code above. The only thing that is happening in this line is an assignment. We're just testing that the .NET assignment operator is working. We don't need to test that. If that's not working, we are going to be having MUCH larger problems. We don't need to cover this line. There is only one path this code can take. There is no branching logic.
Some folks would say that we could rewrite the test to ensure that the CORRECT special is being stored, but this isn't the place for that. The predicate isn't here. Where is it?
return _dailySpecials.Where(s => s.DayOfWeek == dayOfWeek) | ||
.Select(s => s.Special) | ||
.First(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it is. In the GetDailySpecial
method in the MenuUtilities
class. specifically the s.DayOfWeek == dayOfWeek
portion of the code. That is the expression that will return either true or false, and therefore where the flow of the code can branch.
This is the logic that should be tested. Where the predicate is.
[TestMethod] | ||
public void GetDailySpecial_ItIsMonday_ShouldReturnHalfPriceTacos() | ||
{ | ||
MenuUtilities.GetDailySpecial(DayOfWeek.Monday).Should().Be("Half price tacos!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here we are testing it. At least, we're testing one of the possible paths. An interesting aside; code coverage is now telling us that this function is 100% covered, but in actuality, we're only testing one of the possible paths through this method. There are seven "happy path" scenarios - one for each day of the week. There is also a scenario we should consider where the DayOfWeek parameter is passed in as null.
Maybe code coverage isn't the best measurement...
Where should I be looking for predicates and, by extension, places I should be thinking about tests? Some C# examples.
For Linq, some methods can be called both with and without predicates. For example, I would test: |
e4114eb
to
477cf58
Compare
This section will talk about how we identify what code should actually be tested. (hint, code coverage is often not the best measurement for this)