Last week, learned about Virtual environment and Testing. For testing purpose, installed Pytest in Virtual environment.
In this blog, I'm explaining how I did testing for a program, pangram.py, which checks whether a given string is Pangram or not. A Pangram is a sentence which contain all the letters of the alphabet atleast once.The function reads a string, then check presence of each letter of alphabet in the given string. If any of them is not present, the function immediately return with False value. Otherwise it returns True The function is given below,
def pangram(s): for i in "abcdefghijklmnopqrstuvwxyz": if(i not in s): return False return True
Then i did the test program, test_pangram.py. It had two functions,of which one checked if program returns True when string given is a Pangram, and another to checked if program returns False when a string given is not a Pangram. The code is given below.
import pangram def test_pangram_1(): actual_value = classly.pangram("The quick brown Fox jumps over the lazy dog") expected_value = True assert actual_value == expected_value def test_pangram_2(): actual_value = classly.pangram("Hey there this is a blog") expected_value = False assert actual_value == expected_value
On asking our mentor, Noufal Ibrahim, about giving two assertion in single function, he said to keep them in different functions so we get to know which part is not working as expected. So, I kept different functions for True and False conditions. He also mentioned case sensitivity of the function. That is when I found function was True only for Pangram in which all letters are lowercase atleast once. I tested with 'Fox' instead of 'fox', It failed. Then I had to redo the pangram().
As Pangram didn't care about case sensitivity but care about presence of all letters in alphabet, I just used lower() method to convert string to lower case and tested. Then the test was also successful. The function was then as follows:
def pangram(s): s=s.lower() for i in "abcdefghijklmnopqrstuvwxyz": if(i not in s): return False return True
That's all for this week.