473,490 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

NUnit Test

hi
I'm new to NUnit. Is there any way to test a private method with
NUnit?
thanks!

Apr 8 '06 #1
5 2182
The only way is to embed the test in your production class, which one
generally doesn't do.

If your test class is separate from your production code then no, you
cannot directly test a private method.

That said, there is one trick you can use to get _some_ of the desired
effect. If your private method contains a lot of complex business
logic, try splitting the most complicated bits out into one or more
static methods. That is, methods that neither rely on nor change the
state of your object, but only contain logic to do calculations. It's
safe to then make those static methods public, and then you can test
them.

For example, I have an Invoice class that contains a bunch of fancy
logic for calculating due dates. I can test that logic indirectly from
the outside by creating invoices with various terms and then examining
the due dates to see if they're correct.

Or, I can take all of that fancy business logic and put it in some
static methods: I pass them everything they need, they calculate a due
date and return it. I then call those static methods from my Invoice
class in order to calculate due dates, and I can also now make them
public and test them from NUnit without any risk to the internal state
of my Invoice objects.

Apr 8 '06 #2
Not an easy way, look at reflection and you can get hold of private methods,
but a bit of extra work to get there.
"Ether.Sysu" <zs****@yeah.net> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
hi
I'm new to NUnit. Is there any way to test a private method with
NUnit?
thanks!

Apr 8 '06 #3
"Ether.Sysu" <zs****@yeah.net> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
hi
I'm new to NUnit. Is there any way to test a private method with
NUnit?


Not in C#. In C++, you could make the test class a friend of the production
class.
Apr 8 '06 #4
At last, i use class Type's member function InvokeMember(...) to invoke
the private function.

By the way, the code i'm going to test is written by myself. If test
the code by tracing it step by step(F11 in visual studio 2003) is a
good habit?

Sorry for my poor English and thank all of you!

Apr 16 '06 #5
Ether.Sysu <zs****@yeah.net> wrote:
At last, i use class Type's member function InvokeMember(...) to invoke
the private function.

By the way, the code i'm going to test is written by myself. If test
the code by tracing it step by step(F11 in visual studio 2003) is a
good habit?


There are people who swear by tracing through the code line by line
manually. I far prefer unit testing, because it means when I've changed
one bit of code I don't need to run through *everything* manually
again.

I only use the debugger when something isn't working as I expect it to,
after thinking about it for a while. Apart from when I'm investigating
the behaviour of someone else's library, I regard needing to go into
the debugger as a bit of a personal failure. That's not to say it's not
invaluable when you get to that stage, but I tend to think that if what
the code does isn't obvious just by looking at it, then I've got a bit
of a problem beyond it not working properly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 16 '06 #6

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

Similar topics

6
1942
by: Peter Rilling | last post by:
My development team is using VS.NET with VSS integration. They are able to check in and out from within VS.NET. We also use NUnit for testing our libraries. Currently our entire solution is...
2
1752
by: Sky Kim | last post by:
Hi, all. I've know NUnit for a while, now I try to use it. Most of application I develope is data base driven. I want to test data layer most with may input and checking the result, then compare...
2
14224
by: d2d | last post by:
How are you doing there folks? I just have this newbie question about how to compile an NUnit test file from command line using "csc.exe" I installed NUnit 2.1 using *.msi file. I was...
5
4627
by: Naveen Mukkelli | last post by:
Hi all, How can we use "NUnit" in socket programming. I mean, I'm writing a server program which accepts connection requests from the clients. I want to test the number of clients whenever a...
4
2053
by: geoffblanduk_nospam | last post by:
I have an NUnit test plan with well over 500 tests. I now need to produce a test plan document for these tests - documenting them one by one with Word is a painfull task. I was thinking that...
3
1804
by: JJ | last post by:
Hi, In Nunit testing with asp.net are you guys creating a separate Test assembly for testing or integrating right in class themselves? Is anyone using a third party Unit Test addon for Nunit to...
6
8331
by: Ray Tayek | last post by:
hi, i am preparing to teach a class in c++ and would like to intoduce some unit testing. i can make unit tests in c# using this dll and nant from the command line. i am using visual c++ 2005...
1
1785
by: MD | last post by:
Hello I work in a .NET environment and I am about to create a development strategy. As part of this I am looking at implementing a testing tool to fit in with an iterative approach and NUnit...
20
1976
by: Parag | last post by:
Hi, I am trying to figure out best testing tool for my project. I have narrowed down my requirements to two tools NUNIT and VSTS unit. But I have used neither and I have to use only one of them....
6
6049
by: tchaiket | last post by:
Hey all, I'm using NUNIT to test our classes. However, I don't want to hard code test data into the NUNIT classes. For example, to test adding a new Client, I don't want to hard code the...
0
7112
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
6974
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
6852
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
5448
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,...
1
4878
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
4573
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
3084
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...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
277
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.