Python Unit test

4ndre4 4ndre4 at 4ndre4.com.invalid
Sat Jan 30 13:03:42 EST 2016


On 27/01/2016 04:57, toluagboola at gmail.com wrote:
[...]
> Here is what the python code looks like and I am to make a Unittest for it

Just a few ideas: first of all, you have to make your code more modular, 
for it to be unit-testable.

There are a few areas in your code that need testing:

- command line option usage/parsing/validation:
   - test that the parameters passed are syntactically correct
   - test that the parameters passed are semantically correct
   - test that the command line usage shown to the user is correct 
(based on the available options)

This might require the creation of a Usage class that encapsulate the 
usage of getopt. The class could validate the input command line, and 
return a string for the usage. As long as you have a Usage class, you 
can test its behaviours.

- filtering/decoding data:
   - test that a filter is correctly set
   - test that the data are correctly decoded

This might require the creation of a Filter and Decoder classes, the 
former encapsulating the behaviour of your 'pc'; the latter 
encapsulating your 'decode'. The dependency between these two classes 
can be mocked, for example. You can mock the data returned by 
pc.datalink() in your decoding code and make sure they are correctly 
decoded.

And so on...

-- 
4ndre4
"The use of COBOL cripples the mind; its teaching should, therefore, be 
regarded as a criminal offense." (E. Dijkstra)



More information about the Python-list mailing list