473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Re: Unit Testing Techniques

I'm by no means a testing expert, but I'll take a crack at it.

Casey McGinty wrote:
I'm familiar with the unittest module in Python, however I'm hoping
someone can point me to some examples of more advanced usages of the
framework. For example:

1. Using the framework to test a package with nested sub-packages and
modules without having to hard code the location/name of each test module.
I've never run into this.
2. Testing class/methods that depend on file system directory
structures, file data, or values read from system hardware.
Rule of thumb: always separate software from hardware. Write mock
classes or functions that do your hardware/file access that always
return known data (but remember to test for alpha and beta errors--make
sure both valid and invalid data are handled correctly). That way you
can test the client code that is accessing the hardware.

Testing the actual hardware/file access code can get more interesting.
If you're just reading files, your test suite should write the test
files in setUp, try to read them in the test* methods, then clean up in
tearDown. Again, remember to test both good and bad files.

In my shop, we do both hardware and software, and testing the comms
between them can be a real pain. We've done lots of interesting things
like wiring faults into the hardware with switches to connect and
disconnect them, recording command sequences, etc., but I expect this is
beyond the scope of what you're interested in. Just keep in mind that
testing a chunk of code requires at least as much creativity as writing
the code in the first place.
3. Testing class/methods that require simulating functionality from
imported modules.
You can do neat things like this:

import unittest
import mymodule

def my_mock_function():
"""Returns known values."""
class MyTest(unittest.TestCase):
def setUp(self):
self._orig_function = mymodule.function
mymodule.function = my_mock_function

def tearDown(self):
# remember to restore the original function
# unittest doesn't re-import modules
mymodule.function = self._orig_function

def test1(self):
"""Test some code that uses mymodule."""

# etc...

The dynamic nature of Python makes this sort of thing much easier than
other languages.
4. Testing graphical interfaces and associated signal callback functions.
Again, there are lots of strategies you can use; the specifics depend on
you design and the toolkit.

1) You can mock the backend: make sure the right functions get called in
response the user actions.
2) Simulate user actions by manually posting events, calling functions, etc.
3) Mock the gui: make sure the backend is calling all the right GUI
functions.

Try to keep your backend and gui as independent as possible, it will
make testing much easier.
>
Thank you.
------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
I typically make a bunch of different suites that can be run
individually, in various combinations, or all together. Testing I/O
tends to be very slow, so it's nice to be able to turn of these tests
when you're working on other parts of the system.

There are many other testing tools besides unittest. The one I use the
most is coverage.py, to determine if my test suites are hitting all my
code. But you can check out pymock, doctest, nose, etc. too. You may
have to use a combination of tools to meet your needs.

There is also the Testing in Python mailing list
(http://lists.idyll.org/listinfo/testing-in-python). You can probably
get some advice there as well. The more specifics you can give, the better.

As you solve your testing problems, please take the time to post your
solutions (if you can) so we can all learn from your experiences.

-Matt
Jul 11 '08 #1
0 1348

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

Similar topics

4
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...
1
by: Jim Hefferon | last post by:
Hello, Does anyone have suggestions for unit testing CGI scripts? For instance, how can I set the FieldStorage to have certain values? Do I have to go with a dummy FieldStorage class? And is...
19
by: newbiecpp | last post by:
I am looking for a C++ unit testing program. I googled and found there are quite a few there, such as CppTest, C++Test, and DejaGnu. Can someone give me a recommendation among these programs? I...
14
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
4
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
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: ...
4
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...
176
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...
48
by: Ark Khasin | last post by:
Unit testing is an integral component of both "formal" and "agile" models of development. Alas, it involves a significant amount of tedious labor. There are test automation tools out there but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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...

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.