How many tests?

Here is an interesting question, how many tests do you need for the following method?

///<summary> 
///Get the latest published webcast 
///</summary>
public Webcast GetLatest();

Here is how the Webcast entity looks like:

image

Any thoughts?

Print | posted on Tuesday, June 10, 2008 5:46 PM

Feedback


Gravatar

# re: How many tests? 6/10/2008 6:38 PM Charles

None, good programmers write perfect code first time.


Gravatar

# re: How many tests? 6/10/2008 6:49 PM JacobM

Enough to ensure that you are correctly getting the Webcast that has the most recent PublishDate, not simply the one that happens to have been most recently added to wherever the Webcasts are stored. Maybe make sure it works when there are no Webcasts stored, when there is one Webcast, and when there are multiple Webcasts.

Here's a question -- what if the PublishDate is in the future? Should it be returned? If not, that's another test.

What happens if two Webcasts have the same PublishDate? Which one is "the latest"? Whatever the answer, there's a scenario for another test.


Gravatar

# re: How many tests? 6/10/2008 7:20 PM Chris

@JacobM - Yet another case is what should GetLatest() do if (!PublishDate.HasValue)?


Gravatar

# re: How many tests? 6/10/2008 7:28 PM Cory Foy

It's a trick question. It takes as many tests as is necessary to drive out the design.

I'd probably start with something like:

public void GetLatestWebcastCallsGetLatestOfDAO();

since we'll have some sort of data access. I'd also likely have tests in place for equality and hashCode of the Webcast object itself (though it could be abstracted away).

Then, for the DAO, I'd likely have tests in place to make sure that we were calling the data layer correctly, probably by using an in-memory DB. Then we'd need integration tests to make sure everything was working correctly.

The whole nullable DateTime seems interesting - but it also seems like it would be abstracted by the data access layer. If we store the webcasts in a file system, then we have at least the create or last modified date. If they are in the database, then we could use the max primary key, or filter out those records which don't have a publish date.

So, filtering out the test cases for Webcast itself, and the data access layer, then we'd need one - an interaction-based tests which verifies that GetLatestWebcast calls the appropriate DAL method.


Gravatar

# re: How many tests? 6/10/2008 7:29 PM Zack Owens

It depends on what the GetLatest() method's dependencies are. You probably don't have to test the Webcast class unless there is actually code that you have written.. if it's just a bunch of properties, there is no need to test whether the .NET framework does its job.


Gravatar

# re: How many tests? 6/10/2008 7:51 PM Vijay Santhanam

7!


Gravatar

# re: How many tests? 6/10/2008 7:54 PM David Alpert

42; always 42.


Gravatar

# re: How many tests? 6/10/2008 8:29 PM James Curran

It depends on the purpose of the class this method comes from. Is it the ultimate source for information on Webcasts, or is it just your local access point for it.

In other word, does this class hold a handle to a database connection or a RSS feed; or *is* it the database?

If it is the ultimate source, then there are probably a lot that should be done, but say which would require more intimate details of the class.

If it's local the local access point, then you are pretty much limited to range/reasonableness checks on the properties of the returned object. Anything beyond that, you aren't testing the method, your're testing the mocks.


Gravatar

# re: How many tests? 6/10/2008 9:19 PM Richard Lennox

You must not forget the case where there are 0 webcasts or where the colection is null.


Gravatar

# re: How many tests? 6/10/2008 9:40 PM Alex Simkin

Why PublishDate is nullable?


Gravatar

# re: How many tests? 6/10/2008 9:45 PM Scott Bellware

Any attempts to answer this question that aren't phrased along the lines of "can you tell me more about the acceptance criteria" will be addressed by our human resources staff during your exit interview.


Gravatar

# re: How many tests? 6/10/2008 9:51 PM Andyk

I agree David, always 42!! ;)


Gravatar

# re: How many tests? 6/10/2008 9:59 PM Ayende Rahien

Alex,
Because you may want to have a webcast that is not published.

Scott,
LOL


Gravatar

# re: How many tests? 6/10/2008 10:22 PM Thomas Eyde

1, if you accept whitebox tests.


Gravatar

# re: How many tests? 6/11/2008 12:34 AM James Hicks

More information is needed. What are the rules surrounding the publishing of web casts? Can more than one webcast be published on the same date? If so, do you rank them by their Id? How is a null PublishDate handled - is it in the future or in the past?


Gravatar

# re: How many tests? 6/11/2008 4:17 AM Peter Ritchie

Another vote for more information:
Does it throw exceptions? If so, what ones, and why?
Does it GetLatest() modify the state of the instance of the type it's contained in. If so, under what conditions?
Is a null a valid return value. If so, under what conditions will this occur?

...plus the other questions about more information.


Gravatar

# re: How many tests? 6/11/2008 10:13 AM Casey

What is the current datetime ... because the "Latest" may well depend on that too....


Gravatar

# re: How many tests? 6/12/2008 6:29 AM firefly

Scott got it :)

Comments have been closed on this topic.