G
Guest
I’d appreciate some advice regarding unit tests. My questions are general in
nature but (as usual) best conveyed via a (simplified) example:
I have a table that contains two columns – the name of a PC and the
date/time that an event took place.
I have a stored procedure that accepts two date arguments – a start
date/time and an end date/time. It returns a list of PC’s and the number of
times the event took place within the specified time period.
I have a method that accepts three arguments – a dataset, a start date/time
and an end date/time. It populates the dataset by calling the stored
procedure.
I wanted to test my method with some unit tests.
My tests all have the same structure:
• First they update (delete and insert) the necessary records in my table.
• Then they create an “expected†dataset and add the necessary rows to it.
• Then they create an “actual†dataset and pass it to my method (where it is
populated).
• Then they compare the “expected†dataset with the “actual†dataset.
Then I wrote the following tests:
• No records
• One record before start date/time.
• One record at start date/time.
• One record after start date/time.
• One record before end date/time.
• One record at end date/time.
• One record after end date/time.
At this point I was very happy. However, my enthusiasm waned when I began to
consider the “many records†scenario. I realised that, to test every possible
combination of the six “one record†scenarios, I would have to write 36
tests. Worse still, I began to consider the many hundreds of tests required
to test the further combinations involved once multiple computer names were
introduced. Clearly this exponential growth is completely unmanageable, even
within my simply example, let alone more complex real world scenarios.
So my simple question is how can this be achieved efficiently? I have some
ideas of my own but none are obviously the right answer and I wondered what
other developers are doing, does anyone there some good practices they’d like
to share, has anyone read any good books or papers on the subject, etc., etc.
nature but (as usual) best conveyed via a (simplified) example:
I have a table that contains two columns – the name of a PC and the
date/time that an event took place.
I have a stored procedure that accepts two date arguments – a start
date/time and an end date/time. It returns a list of PC’s and the number of
times the event took place within the specified time period.
I have a method that accepts three arguments – a dataset, a start date/time
and an end date/time. It populates the dataset by calling the stored
procedure.
I wanted to test my method with some unit tests.
My tests all have the same structure:
• First they update (delete and insert) the necessary records in my table.
• Then they create an “expected†dataset and add the necessary rows to it.
• Then they create an “actual†dataset and pass it to my method (where it is
populated).
• Then they compare the “expected†dataset with the “actual†dataset.
Then I wrote the following tests:
• No records
• One record before start date/time.
• One record at start date/time.
• One record after start date/time.
• One record before end date/time.
• One record at end date/time.
• One record after end date/time.
At this point I was very happy. However, my enthusiasm waned when I began to
consider the “many records†scenario. I realised that, to test every possible
combination of the six “one record†scenarios, I would have to write 36
tests. Worse still, I began to consider the many hundreds of tests required
to test the further combinations involved once multiple computer names were
introduced. Clearly this exponential growth is completely unmanageable, even
within my simply example, let alone more complex real world scenarios.
So my simple question is how can this be achieved efficiently? I have some
ideas of my own but none are obviously the right answer and I wondered what
other developers are doing, does anyone there some good practices they’d like
to share, has anyone read any good books or papers on the subject, etc., etc.