Write Your First Test
Beginner · Write the code · from Python Testing with pytest: Write Tests That Actually Help
From the lesson: Python Testing with pytest: Write Tests That Actually Help
Imagine you wrote a spell-checker. It works great on the word "hello." But what about "HeLLo"? Or an empty string? Or a number? You won't know until something breaks - unless you write tests.
Read the lessonWrite a function called square that returns the square of a number. Then write a function called test_square that tests it with at least 3 different assert statements (test with a positive number, zero, and a negative number). Call test_square() and print All tests passed! at the end.
# Write the square function
def square(n):
pass
# Write the test function
def test_square():
# Add at least 3 assert statements
pass
# Run the test and print result
More pytest exercises
- Fix the Failing TestBeginner
- Predict the Test ResultsIntermediate
- Test the Edge CasesIntermediate
- Build a Mini Test RunnerAdvanced
- Refactor Repetitive TestsAdvanced