NUnit Test

E

Ether.Sysu

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

Bruce Wood

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.
 
C

Carl-Johan Wik

Not an easy way, look at reflection and you can get hold of private methods,
but a bit of extra work to get there.
 
M

Mike Schilling

Ether.Sysu said:
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.
 
E

Ether.Sysu

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!
 
J

Jon Skeet [C# MVP]

Ether.Sysu said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top