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

Home Posts Topics Members FAQ

Unit Testing: a couple of questions

Hi everybody,

I'm just having a go with Unit Testing for the first time and my
feeling about it in short is: Neat!

I'm a bit worried about the time it's taking me to develop the tests
but after only a day or so I'm already much faster than when I started
with it and the code is already much improved in terms of robustness.
A couple of "philosophi cal" questions have emerged in the process.

1) Granularity
Given a simple class

class myClass():
def __init__(self, data):
__data = data;

def getData(self):
return __data

def setData(self, data):
__data = data

I've been wondering: where do I stop in terms of testing for things
that could go wrong? In this case for example, it might be reasonable
to expand the class to make sure it only receives integers and test
accordingly, i.e.:

def setData(self, data):
try:
data = int(data)
except ValueError:
raise ValueError("Arg ument received cannot be converted to
integer: " + data)

But would it be reasonable to test also for the assignment operators?
After all, if, for some strange reason, there isn't enough memory,
couldn't even __data = data potentially fail?

2) Testing in isolation
I'm not entirely clear on this point. I can see how I need to test
each path of the program flow separately. But should a test -only-
rely on the object being tested and mock ones in supporting roles?
I.e. would this be wrong if SupportObject is not a mockup?

def testObjectToBeT ested _forReallyBadEr ror(self):
supportObject = SupportObject()
objectToBeTeste d = ObjectToBeTeste d()
result = objectToBeTeste d.addSupportObj ect(supportObje ct)
self.failIf(res ult != kSuccess, "Support Object could not be
added!")

I can see how if the SupportObject class had a bug introduced in it,
this test would fail even though it has nothing to do with the
ObjectToBeTeste d class. However, creating mock objects can be quite an
overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
under which it isn't worth doing and a test like the one above is
"good enough".

What do you guys think?

Manu
Oct 28 '08 #1
4 1141
I'm wondering if don't want your class to look something like this:

class myClass():
def __init__(self, data):
self.__data = data

def getData(self):
return self.__data

def setData(self, data):
self.__data = data

For the rest I'll let the experts argue, I don't have a lot of
experience with unit testing either... Shame on me! ;-)

Regards,
antoine

Emanuele D'Arrigo wrote:
Hi everybody,

I'm just having a go with Unit Testing for the first time and my
feeling about it in short is: Neat!

I'm a bit worried about the time it's taking me to develop the tests
but after only a day or so I'm already much faster than when I started
with it and the code is already much improved in terms of robustness.
A couple of "philosophi cal" questions have emerged in the process.

1) Granularity
Given a simple class

class myClass():
def __init__(self, data):
__data = data;

def getData(self):
return __data

def setData(self, data):
__data = data

I've been wondering: where do I stop in terms of testing for things
that could go wrong? In this case for example, it might be reasonable
to expand the class to make sure it only receives integers and test
accordingly, i.e.:

def setData(self, data):
try:
data = int(data)
except ValueError:
raise ValueError("Arg ument received cannot be converted to
integer: " + data)

But would it be reasonable to test also for the assignment operators?
After all, if, for some strange reason, there isn't enough memory,
couldn't even __data = data potentially fail?

2) Testing in isolation
I'm not entirely clear on this point. I can see how I need to test
each path of the program flow separately. But should a test -only-
rely on the object being tested and mock ones in supporting roles?
I.e. would this be wrong if SupportObject is not a mockup?

def testObjectToBeT ested _forReallyBadEr ror(self):
supportObject = SupportObject()
objectToBeTeste d = ObjectToBeTeste d()
result = objectToBeTeste d.addSupportObj ect(supportObje ct)
self.failIf(res ult != kSuccess, "Support Object could not be
added!")

I can see how if the SupportObject class had a bug introduced in it,
this test would fail even though it has nothing to do with the
ObjectToBeTeste d class. However, creating mock objects can be quite an
overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
under which it isn't worth doing and a test like the one above is
"good enough".

What do you guys think?

Manu
Oct 28 '08 #2
For the first bit, a colleague has recently asked the philosophical
question, "How do you test what happens when the power goes down?" :)

In other words, only test the bits that your code does. If you want to
provide type checking, then yes, you have to test that.

It's fair to assume that everything that is *not* your code will work
as expected. If you do find 3rd-party bugs that affect you, then you
should write a unit test that exposes that bug. When they fix it, your
unittest fails, prompting you to remove any workarounds you had.

On Tue, Oct 28, 2008 at 2:56 PM, Emanuele D'Arrigo <ma****@gmail.c omwrote:
Hi everybody,

I'm just having a go with Unit Testing for the first time and my
feeling about it in short is: Neat!

I'm a bit worried about the time it's taking me to develop the tests
but after only a day or so I'm already much faster than when I started
with it and the code is already much improved in terms of robustness.
A couple of "philosophi cal" questions have emerged in the process.

1) Granularity
Given a simple class

class myClass():
def __init__(self, data):
__data = data;

def getData(self):
return __data

def setData(self, data):
__data = data

I've been wondering: where do I stop in terms of testing for things
that could go wrong? In this case for example, it might be reasonable
to expand the class to make sure it only receives integers and test
accordingly, i.e.:

def setData(self, data):
try:
data = int(data)
except ValueError:
raise ValueError("Arg ument received cannot be converted to
integer: " + data)

But would it be reasonable to test also for the assignment operators?
After all, if, for some strange reason, there isn't enough memory,
couldn't even __data = data potentially fail?

2) Testing in isolation
I'm not entirely clear on this point. I can see how I need to test
each path of the program flow separately. But should a test -only-
rely on the object being tested and mock ones in supporting roles?
I.e. would this be wrong if SupportObject is not a mockup?

def testObjectToBeT ested _forReallyBadEr ror(self):
supportObject = SupportObject()
objectToBeTeste d = ObjectToBeTeste d()
result = objectToBeTeste d.addSupportObj ect(supportObje ct)
self.failIf(res ult != kSuccess, "Support Object could not be
added!")

I can see how if the SupportObject class had a bug introduced in it,
this test would fail even though it has nothing to do with the
ObjectToBeTeste d class. However, creating mock objects can be quite an
overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
under which it isn't worth doing and a test like the one above is
"good enough".

What do you guys think?

Manu
--
http://mail.python.org/mailman/listinfo/python-list


--
or*****@orestis .gr
http://orestis.gr
Oct 28 '08 #3
Mel
Emanuele D'Arrigo wrote:
I'm a bit worried about the time it's taking me to develop the tests
but after only a day or so I'm already much faster than when I started
with it and the code is already much improved in terms of robustness.
A couple of "philosophi cal" questions have emerged in the process.
[ ... ]
But would it be reasonable to test also for the assignment operators?
After all, if, for some strange reason, there isn't enough memory,
couldn't even __data = data potentially fail?
I would say, don't test for anything you can't fix. If you have (what the
agile-programming people call) a "user story" describing how your program
will recover from out-of-memory, then test whether that recovery is carried
out. Otherwise forget it.

Best think of unit tests as your sub-program spec written in a different
form: i.e. as executable programs. You don't test for "things that can go
wrong", you test for behaviour that your program must exhibit.
>
2) Testing in isolation
I'm not entirely clear on this point. I can see how I need to test
each path of the program flow separately. But should a test -only-
rely on the object being tested and mock ones in supporting roles?
I.e. would this be wrong if SupportObject is not a mockup?

def testObjectToBeT ested _forReallyBadEr ror(self):
supportObject = SupportObject()
objectToBeTeste d = ObjectToBeTeste d()
result = objectToBeTeste d.addSupportObj ect(supportObje ct)
self.failIf(res ult != kSuccess, "Support Object could not be
added!")

I can see how if the SupportObject class had a bug introduced in it,
this test would fail even though it has nothing to do with the
ObjectToBeTeste d class. However, creating mock objects can be quite an
overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
under which it isn't worth doing and a test like the one above is
"good enough".
I have heard people talk, matter-of-factly about support objects that got so
complicated that they needed unit tests of their own. Of course, anything
can be done in a ridiculous way, but good comprehensive unit tests give you
the eternal assurance that your program is behaving properly, through all
maintenance and modification. That's worth a lot.

Oct 28 '08 #4
Thank you all for the very instructive replies! Much appreciated!

By the sound of it I just have to relax a little and acquire a little
bit more experience on the matter as I go along. =)

Thank you again!

Manu
Oct 30 '08 #5

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...
1
2550
by: serge | last post by:
I've started researching on Unit Testing and I must admit I had never heard of Unit Testing until a couple of months ago. Obviously I am interested in Unit Testing Stored Procedures. I read the TSQLUnit documentation (not all of it) and i also ran into a newsgroup post saying TSQLUnit is very small compared to NUnit. The conclusion I am making out of this post is that I should rather spend time resarching/reading about NUnit than...
2
3218
by: Naveen Mukkelli | last post by:
Hi, I'm writing a client/server application using C#. The server accepts connecitons asynchronously, using BeginAccept/EndAccept. Is there any way we can write some unit tests(NUnit) to test the behaviour of accepting connections and testing some other private methods that would be called when the server receives a connection request.
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.
7
1879
by: Diffident | last post by:
Hello All, Can anyone please suggest me a good unit testing tool. I have seen NUnit but not sure on how I can use it to test my methods which involve session variables, viewstate variables, textbox values. I understand that NUnit is more suitable for OO methods which take set of parameters and return an output parameter. How about tools for testing methods which use state variables and form values?
176
8358
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 their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
4
1507
by: cmay | last post by:
I have been looking on and off for the past 6 months or so for some kind of guidance for creating asp.net unit tests with Visual Studio Team System, but I have yet to find anything significant. Does anyone know of any books/articles that address this? Right now I created a very basic page and created unit tests for it only to find that my webcontrols are not initialized when running the unit tests. So either I am doing something wrong...
10
3921
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 py.test and nose. Are there other options? Is there any kind of concensus about the best, or at least how they stack up to each other? Brendan
0
8685
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
9171
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
9032
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
8905
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
7743
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
6532
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
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3053
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 we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.