Pytest Day 1 of 3
Day 10 of 100
I have always been curious about writing tests for my code and I understand the importance. I have even "written" some when I went through the Flask Mega Tutorial but I didn't understand it and struggled when I tried to write my own. pytest
seems like a more understandable library and I was able to write a few basic tests for the fizzbuzz script before watching the video. There were a lot of things in the guessing game code and tests that I understood what was going on, but I don't think I would be able to write tests like that without diving in to pytest and some pytest background videos. We'll see how the next two days go. Here is the code for my simple fizz_buzz.py
and the tests I wrote:
def fizzbuzz(num):
'''
Returns a phrase based on the divisibilty of a number by 3 and/or 5.
:param
arg1 : num : int to evaluate
:return:
str 'fizz buzz' if num is divisible by both 3 and 5
str 'fizz' if num is divisible by only 3
str 'buzz' if num is divisible by only 5
int num of num is not divisible by 3 and/or 5
'''
if num % 5 == 0 and num % 3 == 0:
return f'{num} fizz buzz'
elif num % 3 == 0:
return f'{num} fizz'
elif num % 5 == 0:
return f'{num} buzz'
else:
return num
for num in range(100):
print(fizzbuzz(num))
And the tests:
from fizz_buzz import fizzbuzz
def test_fizzbuzz():
assert fizzbuzz(1) == 1
assert fizzbuzz(3) == '3 fizz'
assert fizzbuzz(5) == '5 buzz'
assert fizzbuzz(15) == '15 fizz buzz'
The tests that were written in the solution that contained a list of inputs and expected results which reduced the number of lines in the code. If I can't wrap my head around the code in the challenge, I might modify this little program to take user input, validate the input, and then write tests modeled on the guessing game demo that was in the challenge video.