This is not concerned to any examination on test that a normal human take in his socially active and academically not so happening life. This is about the test which every code has to go through. Every line of code, each innocent variable , every conditional is a prime suspect of breaking the code unless and until they pass test to be precise enough unit test.
Every code is guilty , you have to assume that and then only you can write good test cases you have to think about every possibility that can break your code.
During Pycon DevSprint 2015 Kushal said something and that is what I am reminded of whenever I write test case , he said,
Test case which fails when your code is not there is the best test for the code you write.
I was introduce recently to unit test when recently I had to write test case for the feature I am writing for Pagure , you can check the previous articles for that.
Then coincidentally Kushal gave me and Trishna opportunity to write non-gating tests for fedora Atomic and cloud images. This intrigued me much, Tunir is basically a tool that Kushal wrote to automate the whole testing process for cloud images , you can read more about Tunir at Life of Tunir.
Well after that I began to explore more about unittest and wrote some test for Tunir , even you can volunteer for that check out the repo and Trishna’s Blog for more information.
Okay lets get our hands dirty and write some unit test :
This is very basic of unit test :
Step 1: Write this code in a file here I am writing it in python_program.py :
File to be tested
Step 2: Now create a different file and name it test_python_program.py:
How it should run!
**`Now you can see the the output the way you should run the test in python.
Try making self.assertEqual("Hey", out)
and thou shalt pass!`**
If above changes are done the test will pass as assertEquals just compares out and "Hey". This is one of the simplest test that you can write and experiment with it. Hope that helps you. Keep Testing!