473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

"Writing Unit Tests" - What does That Mean - Specifically

I understand what a unit test is. No problem there.

What I'm wondering is what, specifically, do most of you more experienced
developers mean by "writing" a unit test. Am I correct to believe that you
are referring to some way you *document* your tests? If yes, can you provide
some example? Given the huge amount of unit tests that go on with any non
trivial application dev project I'd like to think that there is some
standard template that you follow (home grown, as it may be). At least
that's what I'd like to come up with as I try to improve my programming
habits.

Thanks!
Jul 19 '06 #1
6 1462
Look into NUNIT (nunit.org) or a similar unit testing framework. You'll
actually create (write) test classes.
Jeremy wrote:
I understand what a unit test is. No problem there.

What I'm wondering is what, specifically, do most of you more experienced
developers mean by "writing" a unit test. Am I correct to believe that you
are referring to some way you *document* your tests? If yes, can you provide
some example? Given the huge amount of unit tests that go on with any non
trivial application dev project I'd like to think that there is some
standard template that you follow (home grown, as it may be). At least
that's what I'd like to come up with as I try to improve my programming
habits.

Thanks!
Jul 19 '06 #2
Writing a unit test involves writing code that tests your existing code
for faults.

the classic example is to use some unit testing framework to verify that
the example add(int a, int b) method actually returns the correct answer.

in Java it goes like this:

public void testAdd() {
int a = 5;
int b = 4;
int result = add(a,b);
assertTrue(result == 9);
}

I assume it would be very similar for C# and NUnit. It is all pretty
simple. the assertTrue means that if that add method does not return 9,
assert.

asserts are often used in C when you want to make sure that a condition
is met at some point in your code, thus the NUnit and JUnit method names.

C# and Java are close enough in design that you can glean quite a bit
from JUnit tutorials if you can't find sufficient NUnit documentation.
I'm sure you'll find enough NUnit stuff though.

--
jeremiah();

Jeremy wrote:
I understand what a unit test is. No problem there.

What I'm wondering is what, specifically, do most of you more experienced
developers mean by "writing" a unit test. Am I correct to believe that you
are referring to some way you *document* your tests? If yes, can you provide
some example? Given the huge amount of unit tests that go on with any non
trivial application dev project I'd like to think that there is some
standard template that you follow (home grown, as it may be). At least
that's what I'd like to come up with as I try to improve my programming
habits.

Thanks!

--
jeremiah();
Jul 19 '06 #3
jeremiah johnson wrote:
Writing a unit test involves writing code that tests your existing code
for faults.

the classic example is to use some unit testing framework to verify that
the example add(int a, int b) method actually returns the correct answer.

in Java it goes like this:

public void testAdd() {
int a = 5;
int b = 4;
int result = add(a,b);
assertTrue(result == 9);
}

I assume it would be very similar for C# and NUnit. It is all pretty
simple. the assertTrue means that if that add method does not return 9,
assert.
In c# and NUnit I would write this as:

[Test]
public void TestAdd()
{
Assert.AreEqual(9, add(4,5));
}

Cant remember from JUnit but NUnit will return an error message in the
form of:
Expected: <9>
But was: <5 (If add returned 5 instead of 9).

To me that is clearer than the blank message that gets raised if you
dont specify a message or since I modified my NUnit (gotta love open
source) so all Assert methods call AreEqual (Almost) so the message
would be:
Expected: <True>
But was: <False>

Cheers
JB

<...>
Jul 20 '06 #4
Jeremy <A@B.COMwrote:
I understand what a unit test is. No problem there.

What I'm wondering is what, specifically, do most of you more experienced
developers mean by "writing" a unit test.
Personally, I mean writing the code for a unit test, along with any
comments in/around the code which are required to understand what it's
meant to do.
Am I correct to believe that you are referring to some way you
*document* your tests? If yes, can you provide some example? Given
the huge amount of unit tests that go on with any non trivial
application dev project I'd like to think that there is some standard
template that you follow (home grown, as it may be). At least that's
what I'd like to come up with as I try to improve my programming
habits.
Well, there tend to be naming conventions, but that's about it. I tend
to name my test classes after the class they're testing, but with
"Test" at the end (FooTest testing Foo, for instance) and I tend to
have an instance variable called m_subject of the type I'm testing, but
there's no much beyond that.

Different people will tell you different things about whether to have
the unit tests in the same assembly, but not compiled in release mode,
or to have them in a different assembly. I'm a "different assembly"
guy, although that does make things harder as you can't test internal
members and can't make members available just for testing (other than
with .NET 2.0's "friend" capability).

--
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
Jul 20 '06 #5
So Jon, if you're a "different assembly" kind of guy, what are the
advantages?
You only pointed out the disadvantages of doing it that way, but there
must be a reason you continue to do so.

Is it simply because the test cases are more manageable in their own
assembly?

Jul 20 '06 #6
Steven Nagy <le*********@hotmail.comwrote:
So Jon, if you're a "different assembly" kind of guy, what are the
advantages?
You only pointed out the disadvantages of doing it that way, but there
must be a reason you continue to do so.

Is it simply because the test cases are more manageable in their own
assembly?
That's one aspect - but the main one is that I know that I'm really
running the unit tests against the genuine code. I can take the same
assembly that I'll ship to customers and unit test that, rather than
building two versions, one with the tests in and one without, and
hoping that all the production classes in the shipped version are going
to work the same as they did in the test version.

--
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
Jul 21 '06 #7

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

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
0
by: Emmett | last post by:
The transform below should create a dropdown where the entry for "SSS Building I" is selected. (Or am I missing something? ) This transform creates the dropdown but doesn't do the select. Can...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
5
by: Mateusz Loskot | last post by:
Hi, I'd like to ask how XML parsers should handle attributes which consists of &quot; entity as value. I know XML allows to use both: single and double quotes as attribute value terminator. That's...
5
by: Bob | last post by:
Are they different names for the same concept ?
14
by: Frederick Gotham | last post by:
The original purpose of "inline" was that code could be "expanded in place". Of course, it has other uses... For one thing, the following two translation units will compile together succesfully...
6
by: Mizipzor | last post by:
Hi, this is my first mail to the list (and any list for that matter) so any pointers on errors from my part would be appreciated. Im more used to forums. To start off, Ive read some python...
27
by: duli | last post by:
Hi: I would like recommendations for books (in any language, not necessarily C++, C, python) which have walkthroughs for developing a big software project ? So starting from inception, problem...
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
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,...
1
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
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...
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...
0
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
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
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 ...

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.