[Tutor] Best Code testing practice?

Alan Gauld alan.gauld at btinternet.com
Thu Jun 20 14:52:43 CEST 2013


On 20/06/13 12:43, Matt D wrote:

> Is there a fast way test some piece of code?

There are several testing frameworks for testing Python code.
nose is one example.
But...

> look at the GUI I am making with out changing the file in dir 'baz'

In general your GUI should not be tied into one particular file or 
folder structure. You should be able to specify that either in a config 
file that you read when the GUI starts or as command line arguments.

That way you can separate your test data from your real production
data.

> running the actual program (which is quite extensive).

Hopefully you have separated the presentation logic (the GUI
components) from the processing logic? If so testing the processing 
logic is fairly easy using one of the aforementioned frameworks. Testing 
the GUI itself is more tricky and usually requires
using some kind of robot to mimic what a user does by sending
dummy event messages into the GUI toolkit.

> just have a .py file with only the smallest amount of code possible to
> make the GUI run, and I could run the .py file from the interpreter to
> make the GUI run, this would be beautiful.

That depends on how you built the app. If the GUI is encapsulated
as an an object then you can usually import the GUI code and instantiate 
the object. But if you have hard coded links from the GUI event handlers 
to your processing logic and from there to the real data/files then its 
going to be difficult.

> Do we need an IDE for this sort of thing

Almost certainly not, although it may help.
You never *need* an IDE for python, they are only there to help.

The bottom line is that if your application architecture
has been well designed to separate the GUI and processing
and you have decoupled the data from the logic then testing
should be straightforward. If its all one big organic mashup
then you have a tough time ahead. It may be easier to
re-architect the application and then test it than to
struggle to bolt on tests retrospectively.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list