Need advice on writing better test cases.

Anubhav Yadav anubhav.yadav at gmx.com
Mon Aug 28 04:53:05 EDT 2017


> A good way to learn unit testing and regression testing is to download 
> the Python source code and read the test suites.

It’s a fantastic idea. I will also have a good understanding of the internals 
of the standard library and at I can learn more about testing. 

Any specific module that you recommend to get started with? `collections` module maybe? 
> 
> Also consider using doc testing, for both documentation and unit tests. 
> See the doctest module.

I have seen some modules with these kind of test cases. I thought it’s just a way to test your
code like `unittest`. I will have another look at it. 
> 
> Can you explain what you mean by "functional test cases", and show an 
> example or two of the sorts of tests you would write?

So let’s say I want to write a server which listens to client sending data at 12 seconds interval. 
Here are it’s requirements:

1. Listen to data from all clients. 
2. Never lose any data packet. 
3. If the data is garbage (not following the spec) discard it. 
4. If the data is good, append a timestamp and push it to a rabbitmq queue. 

So what I would like to do is: 

1. Send 100 packets from 1 client and see if the rabbitmq queue has 100 packets. 
2. Send 90 good packets and 10 garbage packets and see if the rabbitmq queue has 90 good packets. 
3. Send 10 packets from 10 different clients at the same time and see if there is correct data in the rabbitmq queues. 
4. So on and so forth. 

This is just one example. Extension would be another module (which has it’s own suite of unittests) will take this data
from queues and do some post processing and put it in some database like `mongo` or `mysql`. 

So the same set of functional tests should start this second module and see if the intended data is getting parsed properly
and delivered to the databases. 

I don’t really know if this is the write way of writing tests. This is just something that I would do if I am told to make sure your 
Software does what it’s intended to do. 

- Anubhav. 


More information about the Python-list mailing list