473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 4385
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.MyObject you would have a
corresponding test fixture in your testing project i.e.
MyTestingProject.MyObjectTestFixture
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
"PrivateObject" 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 InternalsVisibleTo 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****@sturmnet.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 "PrivateObject" 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
InternalsVisibleTo 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****@sturmnet.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
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...
14
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
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/ +--...
6
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...
72
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: ...
10
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...
176
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...
27
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...
10
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
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...

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.