The Fedora meetup was conducted by Kushal Das on 23rd April 2016. Around 15 people attended the meetup at Kushal’s place.
In this meetup, Kushal introduced us to Unit Testing. Its a term often heard in Software development.
Testing is done to determine if there are errors in the code. It does not prove that the code is correct but it just checks that the conditions are handled correctly. Tests are as important as implementation itself.
Unit testing is done to evaluate a particular code component. These components can be function , class etc. It determines how well the component reacts to valid or invalid input. Unit tests are written and executed by software developers.
unittest – an automated testing framework in Python
I installed the unittest module on my Fedora machine using:
pip install unittest
I used unittest module in my python program using:
Basic Test Structure
There are two parts of a basic test:
- The code to prepare for test (test fixture)
- The code to test
Our first Test Case
This program tests cat command in Fedora. The cat command is used to read the contents of a file. We use the cat command in the following way:
This following program is the first test case. It prints:
- the output of cat command
- the error
- the exit code
I saved the script with the name “firetest.py”. The simply ran the script directly from the command line:
The above result shows:
- The output of cat command.
- The command ran successfully so error message is empty
- exit code is “0”(successfully completed)
After that, there is a “.” before the dashed line above which indicates that the test passed.
Next, it shows the amount of time the tests took.
After that, the test outcome i.e. OK is shown.
Every test has 3 possible outcomes:
- OK: the test passes
- FAILED: the test doesnot pass and raises “AssertionError” exception
- ERROR: the test raises error other than “AssertionError”
Now, i did modification to the above program and changed the file name to something else i.e. gave a wrong file name and then ran the script. This is the result:
Here, since the file name is wrong, the cat command throws an error. The output is empty. The error message is displayed along with the exit code “1”(unsuccessful completion).
Kushal told us that the “b” before the output and error message indicates that it is in “bytes”.
To convert the output in string, we inserted the following line before print statement:
Assert statement is used for verifying expectations or some property of code being tested is true. Assert literally means “to state as fact”.
Assert statement stops the routine and sends the error message.
There are many assert statements like:
In the meetup, we tried “assertIn”. assertIn(a, b) checks if a is in b.
This following is the code modification that we made. Here, the assertIn checks if the word “fedora” is present in the output.
This is the result:
This means that the string “Fedora” is present in the output of cat command.
We modified the string “Fedora” to generate an error in this way:
After running the script, this is the result:
The test fails as the string “Fedra” is not present in the output.
Kushal, Chandan Kumar and Praveen Kumar gave all of us a simple task of testing.
My task was to:
- change a file’s SELinux permissions to root (not through mode)
- Write a test case to check if it gives an error when i try to access it through normal user sudo chmod 700
- Check if the error message is present in the SELinux’s log file.
In this meetup i learnt about unit testing in Python; also I learnt about SELinux for the first time while solving the task.
Thanks to Kushal for providing the yummy snacks 🙂 and conducting the meetup.