473,698 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unit Testing Examples

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.)
Up to now, I simply evaluated the validity of a graph by eye-balling
the results of toy problems. Obviously, this gets harder as the graphs
grow, so unit testing sounds like a good way to make sure I catch
mistakes early.

I'm not sure, though, at what level I have to write unit tests. It's
easy to write a tests that validates the consistency of a single node
of a graph, but the real complexity is, of course, in the interaction
of multiple nodes.

What I'm looking for is some examples, existing open source projects
that make heavy use of unit testing. I'd like to see a real life
example about how those tests are structured and how the complexity of
these tests expands from object level to system level.

Thanks,
Tom
Jul 18 '05 #1
6 3682
On 17 Nov 2003 09:10:32 -0800,
Tom Verbeure <to**********@h otmail.com> wrote:
I'm not sure, though, at what level I have to write unit tests. It's
easy to write a tests that validates the consistency of a single node
of a graph, but the real complexity is, of course, in the interaction
of multiple nodes.


Not too long ago I wrote a set of unit tests for a graph data type;
unfortunately the code belongs to my employer and isn't open source, so you
can't see a copy. What I did was:

* Test basic things: create an empty graph and check that it has zero nodes
and edges. Add a vertex and edge; remove the vertex and edge.
In my case there are various accessors for .number_of_node s()
and .number_of_vert ices(), so I checked that they went up by one.
* Create a little graph, ~5 nodes, and test the BFS and DFS methods to see
that they output nodes in a sensible order.

Sancho, the testing framework I use, provides code coverage measurement, so
I used that to find which lines of code weren't exercised by the test suite
and added tests to cover more lines. It's usually difficult to exercise
100% of the code, because error cases can be difficult to provoke,
especially if they represent an internal invariant being broken or an
external resource such as a file or server not being available, and some
trivial accessors aren't worth testing, so I'm usually happy with 70%-80%
coverage as long as the complicated methods are completely or mostly
covered.

In my case I'm not bothering to run a very large graph as a test case,
because the little graph exercises all of the relevant branches; I'm
therefore betting that the code is OK for larger graphs, too. If a large
graph demonstrates a bug someday, I would try to boil it down to a small
test case and add this case to the test suite.

If performance is important, you might consider adding a performance test:
run an algorithm on a large graph and check that the running time is
suitably fast. This might save you from making some change that doesn't
affect correctness but results in a sizable performance regression.

--amk
Jul 18 '05 #2
- Tom Verbeure <to**********@h otmail.com> :
snip
What I'm looking for is some examples, existing open source projects
that make heavy use of unit testing. I'd like to see a real life
example about how those tests are structured and how the complexity of
these tests expands from object level to system level.


IMHO Twisted Matrix is a good example.
www.twistedmatrix.com

--
__Nicholas Wieland
Errors should never pass silently.
Unless explicitly silenced.
__The Zen of Python
Jul 18 '05 #3
to**********@ho tmail.com (Tom Verbeure) writes:
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.
Graphs aside, it's easier 1. when you write them before you write the
code 2. when you get some practice (of course).

[...] I'm not sure, though, at what level I have to write unit tests. It's
easy to write a tests that validates the consistency of a single node
of a graph, but the real complexity is, of course, in the interaction
of multiple nodes.

[...]

Test at both levels. Also, even for a graph object containing nodes
(if that's how it's written), it can make sense to test the graph
object independently of the nodes. Sometimes you look at tests like
that and think "how could that possibly fail?", but it's surprising
how often they do if you do any refactoring (and even bug fixing,
sometimes). This is related to "mock objects", though I think mock
objects are worth avoiding if they don't turn out to be necessary.

I certainly don't like to put this forward as a beacon of good
practice, but I was quite unhappy with the tests in ClientForm (too
long, too apparently repetitive and trivial), *until* I wanted to port
it from an event-based HTML parser to a DOM. I was amazed at the
number of tests that failed in subtle ways. If I didn't have the
tests, I would never have finished it, I'm quite sure, and the tests I
thought were not pulling their weight turned out to be valuable.
Elegant graph algorithms and messy W3C standards (both HTML DOM and
HTML are quite nasty) are different things, and the latter has more to
benefit from unit tests, but I think it's worth experimenting with the
more paranoid end of unit testing. One thing I don't do and I'm quite
sure is valuable is code coverage measurement, so if I were starting
afresh I'd be attracted to amk's framework (though there are several
tools to do it with unittest -- they're just not integrated to make it
trivial).

As a BTW, one interesting thing about moving from an event parser to a
DOM (and kept the old implementation: the event-based code is still in
use) was that the unit-test nature of some of the tests was actually a
hindrance to getting the tests running, and a barrier to
maintainability . Necessary background: the module parses HTML to get
a list of HTMLForm objects; HTMLForms contain Control objects
(controls are things like text entry boxes, buttons, menus,
checkboxes). Now, my tests of the various Controls constructed
Controls directly rather than using the parser -- these were supposed
to be unit tests, after all, so I didn't want to be testing the
Controls *and* the parser at the same time. The problem, of course,
was (still is, I haven't fixed it yet) that when I started using the
DOM, the constructors for the Control objects changed completely, and
I had to fork the test code in a nasty way just to get the tests to
run. If I'd just been lazy and used the parser interface to build
Control objects, I could have run the tests with almost no changes.
Not entirely sure what the moral of this story is yet... maybe just.
John
Jul 18 '05 #4
Am Mon, 17 Nov 2003 09:10:32 -0800 schrieb Tom Verbeure:
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.


If you are using objects which are containers for
other objects, you could do it like this:

Every object gets a method called "unittest". This performs
a unittest on its own data, and then calls unittest() for
all child objects.

This way you can check all objects by calling unittest for the root object,
or call the method for only a part.

Be sure to do no recursion if you have circular data.

thomas


Jul 18 '05 #5
Thanks all for your suggestions!

Tom
Jul 18 '05 #6
to**********@ho tmail.com (Tom Verbeure) wrote in message news:<e4******* *************** ****@posting.go ogle.com>...
Thanks all for your suggestions!

Tom


Sorry to be late to get to this thread, but I have a Python O/S
project at sourceforge.net/projects/d-rose that strives to have
complete unit testing, and is a simulation that uses/is-based-on a
graph/network. Hope this is of some help to you as an example, and
feel free to ask if you have any questions. I certainly won't claim
that mine is the best example of such, as I'm just learning TDD, too,
but...

Dave
Jul 18 '05 #7

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

Similar topics

4
3738
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is possible). Most of my programming these days involves using PHP for creating script files for automating tasks and procedures (locally), and also for anything that might be needed by our divisional Intranet (not a huge site by any stretch of the...
38
3403
by: Christoph Zwerschke | last post by:
In August 2001, there was a thread about the "Art of Unit Testing": http://groups.google.com/group/comp.lang.python/browse_frm/thread/aa2bd17e7f995d05/71a29faf0a0485d5 Paul Moore asked the legitimate question why there is no hook for a "global" fixture code that is run only once for the whole TestCase, as opposed to the normal "setUp" and "tearDown" code that is run for every single test in the TestCase. A "global fixture" would be...
14
2745
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
11
2300
by: D. Yates | last post by:
Hi, In an effort to improve my own code and try to teach others that I work with that unit testing IS MANDATORY. I'm interested in examples of unit testing that other people are using. How many folks rely on NUNIT? Do you create a testing class per application, per class, per assembly to exercise your code?
4
2162
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
5246
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.
4
18966
by: Dat AU DUONG | last post by:
Hi, I am new to Unit testing, could you tell me where I could find information (hopefully step by step) and what is the benefit of unit testing. I am a sole developer in a company, therefore I don't get expose to much of this technology. Thanks in advance. Regards Dat.
0
924
by: cmay | last post by:
I am already familar with the concept of unit testing and TDD, but what I am looking for is something that talks about how you could/should use unit testing in asp.net apps. Most article out there have such simple examples... call the AddTwoNumbers method and assert that the result is correct, but stuff like this never seems to apply to any asp.net apps I work on. Is there any real world examples out there of how to take an asp.net...
0
1238
by: noway001 | last post by:
Hi, I need to learn how to do unit tests for certain functions. I have googled and come accross some stuff, but all the examples are easy. I read multiple articles on code project, but it didn't help. Usually, I see examples of unit testings functions that take in 1-2 parameters and perform addition/multiplication/etc. I need to know how to unit test functions that return nothing, take in no parameters, and so on. For example, how do I...
0
8674
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
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9026
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...
1
8893
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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
7723
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
6518
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
4366
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...
3
2001
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.