473,503 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unit testing in a controlled loop

Here's my problem.
Have a database interface with several methods.
I created a database class to implement the interface,
and a set of unit tests to test each method.
Now I want to support another database provider.
The implementation is very easy: I simply
create a new database class, and implement all the methods using the new
database provider.
My question is: How do I get this same set of unit tests to run in a loop?
My problem is that I have one test class with many test methods,
and I want the testing framework to run each of those tests once for every
different database implementation that I've coded.
I certainly could have a single test method that has a loop that calls a
hand-crafted set of test method's.
But that would put the looping and every single test method in one single
unit test method. Then I would lose the nice partitioning that the
conventional test architecture provides.
I know this may be confusing when you read it.
Just imagine one of your typical unit tests. Now imagine you want to repeat
all of those unit tests exactly as they are structured, except you want to
change one single variable for each run.
Thanks
--
David K Allen
Carlson School of Management
University of Minnesota
Minneapolis, MN
Mar 1 '06 #1
5 3903
David,
How do I get this same set of unit tests to run in a loop?
I wouldn't think of it that way. What I would do is to have a base
test-class with an instance variable (testObject) of the type of the
interface that you have implemented. That base class has all of the test
methods that test the proper implementation of the interface. In the
"SetUp" method, instantiate the right implementation class and cast it to
the interface and store the reference in the testObject variable. In the
"TearDown" method, clean the testObject up. All of the test methods should
test against the object stored in the testObject instance variable.

Then I would subclass that base for each database implementation and inherit
all of the testing from the base test-class. Then override "SetUp" and
"TearDown" and any other test methods appropriately. This will effectively
test all of the different database implementations that you want to support
without having a ton of duplicated test code; and without a looping
structure.
My problem is that I have one test class with many test methods,
and I want the testing framework to run each of those tests once for every
different database implementation that I've coded.


The architecture that I have described above does this.
Regards,

Randy
Mar 1 '06 #2
Thanks, Randy.
This will indeed work.
But I must create test methods in each inheritor and decorate them with the
[TestMethod] attribute. As you suggest, those test methods will call the
parent where the interesting code can exist (at least we avoid duplicating
that part, which would be tedious and error-prone.
But I was hoping to avoid duplicating the method shells as well.
If I'm going to do this, I don't even need inheritance,
I can use composition.

Anyway, when I add a new method to the interface, I have to add the complex
logic once (good) but I have to create a shell method in every database
implementation.

A looping approach would allow me to create only one test method.
thanks
--
David K Allen
Carlson School of Management
University of Minnesota
Minneapolis, MN
"Randy A. Ynchausti" wrote:
....
Then I would subclass that base for each database implementation and inherit
all of the testing from the base test-class. Then override "SetUp" and
"TearDown" and any other test methods appropriately.

....

Mar 1 '06 #3
If your new database class has same methods and properties, you may have a
collection containing all your database objects, fill the collection in
TestInitialize() method. In a test method, test all objects in the
collection.

Anyway, Unit test is intend to test a module or a class. For multiple
classes, I think it is better to create multiple Uint tests since you may
get different results from different database provider.
Luke Zhang
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Mar 1 '06 #4
Thanks. Between your suggestion and Randy's, I have a nice range of choices.
I get your point about unit testing.
But in this particular case, I am really testing that each implementation
confroms to the same interface and I expect them all to honor the same
contracts, regardless of how they implement them.

So to summarize the patterns:

If uniformity of behavior is expected from every implementation,
and there may be several implementations,
then creating a collection in TestInitialized and having every test loop
over every item is the easiest way to handle the change "add a new
implmentation and ensure it conforms."

If some variety is expected in behavior from implementation to implementation,
then having a common class with shared methods is the easiest way to handle
changes, because copy and paste isn't so bad and it allows tailoring certain
methods that vary.

David K Allen
Carlson School of Management
University of Minnesota
Minneapolis, MN
"Luke Zhang [MSFT]" wrote:
If your new database class has same methods and properties, you may have a
collection containing all your database objects, fill the collection in
TestInitialize() method. In a test method, test all objects in the
collection.

Anyway, Unit test is intend to test a module or a class. For multiple
classes, I think it is better to create multiple Uint tests since you may
get different results from different database provider.
Luke Zhang
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Mar 1 '06 #5
This is a good summary on this issue. Thank you for sharing.

Luke Zhang
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Mar 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3722
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is...
11
2304
by: rhat | last post by:
Hi Everyone, I've recently been reading some articles about unit-testing in Python , but I am a bit confused: where do I go to get started with this? I tried googling for "unittest" but all I've...
14
2709
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
4
2141
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
5154
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
4
18944
by: Dat AU DUONG | last post by:
Hi, I am new to Unit testing, could you tell me where I could find information (hopefully step by step) and what is the benefit of unit testing. I am a sole developer in a company, therefore I...
176
8186
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
48
2446
by: Ark Khasin | last post by:
Unit testing is an integral component of both "formal" and "agile" models of development. Alas, it involves a significant amount of tedious labor. There are test automation tools out there but...
20
2395
by: earthwormgaz | last post by:
Hello, I'm after doing some C++ unit testing, I'm using CppUnit. I like the look of RudeMocks, but it doesn't work for Solaris/Sparc, so its no good to me sadly. So, I have class A, and it...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.