473,883 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unit test best practice

Hi,

I want to add unit tests in my application using NUnit.
At first, I thought to add unit tests in the class that is tested, enclosed
in a conditional attribute to remove these unit tests from release code.
Though, there is (at least) 2 problems working like that :

1) the class can becoming very large if there is a lot of tests
2) when releasing, the NUnit dll should not be needed => it should not be
referenced.

How do you manage this ?

Thanks in advance for your help.
Nov 17 '05 #1
6 4410
Hi Droopy,
when writing your unit tests put them inside a seperate project and
reference the libraries you want to test from your test project. This way
your test project will have a reference to the NUnit.Framework DLL and will
contain all of your testing code, your production project will then have no
code relating to testing or any unneccessary references.

For each class you want to test i.e. MyLibrary.MyObj ect you would have a
corresponding test fixture in your testing project i.e.
MyTestingProjec t.MyObjectTestF ixture
Hope that helps
Mark R Dawson
http://www.markdawson.org

Some of my thought on unit testing
http://www.markdawson.org/software/unittesting

"Droopy" wrote:
Hi,

I want to add unit tests in my application using NUnit.
At first, I thought to add unit tests in the class that is tested, enclosed
in a conditional attribute to remove these unit tests from release code.
Though, there is (at least) 2 problems working like that :

1) the class can becoming very large if there is a lot of tests
2) when releasing, the NUnit dll should not be needed => it should not be
referenced.

How do you manage this ?

Thanks in advance for your help.

Nov 17 '05 #2
Droopy wrote:
I want to add unit tests in my application using NUnit.
At first, I thought to add unit tests in the class that is tested, enclosed
in a conditional attribute to remove these unit tests from release code.
Though, there is (at least) 2 problems working like that :

1) the class can becoming very large if there is a lot of tests
2) when releasing, the NUnit dll should not be needed => it should not be
referenced.

How do you manage this ?


I use separate assemblies for my unit tests, usually one per
implementation assembly. These assemblies reference the tested assembly,
the NUnit assembly and everything else needed for the purpose of testing.

Of course it can be a bit difficult to access object properties or methods
that aren't publicly available in such a scenario, but I tend to believe
that it's not usually the best idea to test non-public members at all. For
those instances where it seems to be a good idea, I have my own
"PrivateObj ect" implementation, a class that wraps another class and
allows access to non-public members - in VS 2005, AFAIK, there's a similar
thing readily available and you can also use InternalsVisibl eTo attribute
to allow your testing assembly access to stuff inside the implementation
assembly.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #3
I agree with the other posters. I keep my testing code separate from the
actual release code. I actually keep the testing code in a parallel solution
with parallel projects as my release code to make things easier. One thing I
did want to point out is that just because you reference an assembly doesn't
mean it'll actually be loaded. .NET assemblies work the same as delay-loaded
DLLs. A .NET assembly will only be loaded when an object contained in the
assembly is needed so unless you reference a test object NUnit doesn't need
to be shipped although it will, by default, be copied to your deployment
directory when you build it in VS. Still I don't like having any "excess"
references so keep your test projects separate from your release projects.
Note however that VS 2005 will automatically add a test project to your
solution when you elect to auto-generate test cases for a class. You'll then
need to move the project elsewhere.

Michael Taylor - 9/5/05

"Droopy" wrote:
Hi,

I want to add unit tests in my application using NUnit.
At first, I thought to add unit tests in the class that is tested, enclosed
in a conditional attribute to remove these unit tests from release code.
Though, there is (at least) 2 problems working like that :

1) the class can becoming very large if there is a lot of tests
2) when releasing, the NUnit dll should not be needed => it should not be
referenced.

How do you manage this ?

Thanks in advance for your help.

Nov 17 '05 #4
OK thanks for your answer.

I was just creating separate assemblies that reference the tested
assembly and NUnit assembly.
The only problem left is with the main GUI application that is a .exe
file => it cannot be referenced by the test assembly.
May be it is not a good idea to test the GUI ?
Ideally, perhaps I should use interfaces between GUI and "business
logic" code and only test these interfaces ?
"Oliver Sturm" <ol****@sturmne t.org> wrote in
news:xn******** ********@msnews .microsoft.com:
Droopy wrote:
I want to add unit tests in my application using NUnit.
At first, I thought to add unit tests in the class that is tested,
enclosed in a conditional attribute to remove these unit tests from
release code. Though, there is (at least) 2 problems working like that
:

1) the class can becoming very large if there is a lot of tests
2) when releasing, the NUnit dll should not be needed => it should not
be referenced.

How do you manage this ?


I use separate assemblies for my unit tests, usually one per
implementation assembly. These assemblies reference the tested
assembly, the NUnit assembly and everything else needed for the
purpose of testing.

Of course it can be a bit difficult to access object properties or
methods that aren't publicly available in such a scenario, but I tend
to believe that it's not usually the best idea to test non-public
members at all. For those instances where it seems to be a good idea,
I have my own "PrivateObj ect" implementation, a class that wraps
another class and allows access to non-public members - in VS 2005,
AFAIK, there's a similar thing readily available and you can also use
InternalsVisibl eTo attribute to allow your testing assembly access to
stuff inside the implementation assembly.
Oliver Sturm


Nov 17 '05 #5
Droopy wrote:
I was just creating separate assemblies that reference the tested
assembly and NUnit assembly.
The only problem left is with the main GUI application that is a .exe
file => it cannot be referenced by the test assembly.
May be it is not a good idea to test the GUI ?
Of course this is just a personal preference, but I like to have all the
code that does more than just glue together other parts in a DLL assembly.
The glue code in the final EXE assembly that makes up the application
would be quite hard to test with NUnit, I believe, and in this way I get
to use unit tests for allmost all parts of my code.

I do use AutomatedQA's TestComplete to test gui stuff, though.
Ideally, perhaps I should use interfaces between GUI and "business
logic" code and only test these interfaces ?


Yes, I think that's what you should do.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #6
"Oliver Sturm" <ol****@sturmne t.org> wrote in
news:xn******** ********@msnews .microsoft.com:
Droopy wrote:
I was just creating separate assemblies that reference the tested
assembly and NUnit assembly.
The only problem left is with the main GUI application that is a .exe
file => it cannot be referenced by the test assembly.
May be it is not a good idea to test the GUI ?


Of course this is just a personal preference, but I like to have all
the code that does more than just glue together other parts in a DLL
assembly. The glue code in the final EXE assembly that makes up the
application would be quite hard to test with NUnit, I believe, and in
this way I get to use unit tests for allmost all parts of my code.

I do use AutomatedQA's TestComplete to test gui stuff, though.
Ideally, perhaps I should use interfaces between GUI and "business
logic" code and only test these interfaces ?


Yes, I think that's what you should do.
Oliver Sturm


OK thanks for your time.
Nov 17 '05 #7

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

Similar topics

6
3693
by: Tom Verbeure | last post by:
Hello All, so I'm now convinced that unit testing is really the way to go and I want to add it to an existing project. The first thing that I find out, is that, well, it's hard work. Harder than writing the real functional code. My application manipulates graphs. I first write the skeleton in Python, then convert it to C++ for high performance (some graphs can have millions of nodes.)
14
2767
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
2
2077
by: Ben Finney | last post by:
Howdy all, My practice when writing unit tests for a project is to make 'test/' subdirectories for each directory containing modules I want to test. project-foo/ +-- lib/ | +-- test/ +-- data/ +-- gui/
6
2706
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following test-driven development, writing tests and then coding to satisfy them, I'll start with some of the simple tests for a class.
72
5296
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: http://geosoft.no/development/unittesting.html Thanks.
10
4526
by: Sean Chambers | last post by:
Hello, I am attempting to utilize the MVP pattern in a new app I am building. I am using TDD, along with mock views attached to the View interface to attempt to unit test the UI. The problem I am having is when using the InitView() of the presenter, normally I would be retrieving data from NHibernate when InitView is called. Therein lies the problem, I can't seem to figure out how to properly
176
8535
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 their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
27
2582
by: brad | last post by:
Does anyone else feel that unittesting is too much work? Not in general, just the official unittest module for small to medium sized projects? It seems easier to write some quick methods that are used when needed rather than building a program with integrated unittesting. I see the value of it (the official unittest that is)... especially when there's a lot of source code. But this... if len(x) != y: sys.exit('...')
10
3940
by: Brendan Miller | last post by:
What would heavy python unit testers say is the best framework? I've seen a few mentions that maybe the built in unittest framework isn't that great. I've heard a couple of good things about py.test and nose. Are there other options? Is there any kind of concensus about the best, or at least how they stack up to each other? Brendan
0
9948
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10767
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7980
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.