In this exercise, you'll learn how to create test cases for a given block of code using PyTest.
You will be checking the accuracy of a string input to a given function against some conditions and writing two functions:
- The first function will check if the length of the input string is within a
specific limit of words and characters. - The second function will check if the basic grammar of the string is well-defined.
- View -> Editor Layout -> Two Columns
- To view this file in Preview mode, right click on this README.md file and
Open Preview
- Select your code file in the code tree, which will open it up in a new VSCode tab.
- Drag your assessment code files over to the second column.
- Great work! You can now see instructions and code at the same time.
- Select your Python file in the Visual Studio Code file tree
- You can right click the file and select "Run Python File in Terminal" or run the file using the smaller
play button in the upper right-hand corner of VSCode.
(Select "Run Python File in Terminal" in the provided button dropdown)- Alternatively, you can follow lab instructions which use python3 commands to run your code in terminal.
Ensure the string variables that will be passed as arguments to the code are within a specified length and have a well-defined structure.
-
Open the
test_spellcheck.py
file inside the project folder. -
Import the
pytest
andspellcheck
modules. -
Comment out the beta variable using # symbol for now.
-
Next, complete the
test_length()
andtest_struc()
functions.
These two functions use input_value to check if the functions defined in spellcheck behave correctly. -
In
test_length()
function, you must add two assert statements.
In each assert statement you first need to call the required function from the spellcheck file that you imported,
and then check against some conditions. For example, the format will be similar to the following against some condition:assert spellcheck.some_function(input_value)
- 5.1: Add the first assert statement over
function word_count()
from the main code which asserts that the returned value is less than 10. - 5.2: Add the second assert statement over
function char_count()
from the main code which asserts that the returned value is less than 50.
- 5.1: Add the first assert statement over
-
In the second function
test_struc()
, you must add two assert statements. The first assert statement checks if the first character is in upper case.
The second assert statement checks if the sentence or the string variable passed ends with a dot (“.”)- Add the first assert statement over function
first_char()
from the main code.
Now call a built-in functionisupper()
directly over it, such asfunction_name.isupper()
. isupper()
function returns True if it is called over an upper-case character and False if called over a lower-case character.
For example,"A".isupper()
returnsTrue
and"a".isupper()
returnsFalse
.- Add the second assert statement over the function
last_char()
from the main code and compare it to“.”
- Add the first assert statement over function
-
Save the files.
-
Open the terminal to execute the files.
-
Run the code using the following command (within the project directory):
python3 -m pytest test_spellcheck.py
-
Both the tests should pass in this case.
- BONUS STEP:
Pass the variable beta instead of alpha in all four of the functions.
The result should now show one passed and one failed test.
Tips
Be sure to double check some common mistakes made in this process below before submitting!
- Forgetting to import the
pytest
andmain
code file- Not passing the variable names correctly