Below is a fizzbuzz program done TDD using unittest in one file, I run it in the command line using python fizzbuzz.py -v.
import unittest
class FizzBuzz():
def fizz_buzz(self, number):
if number % 3 == 0 and number % 5 == 0:
return 'fizzbuzz'
if number % 3 == 0:
return 'fizz'
if number % 5 == 0:
return 'buzz'
return str(number)
class FizzBuzzTests(unittest.TestCase):
def setUp(self):
self.fb = FizzBuzz()
def test_one_gets_one(self):
self.assertEqual('1', self.fb.fizz_buzz(1))
def test_three_gets_fizz(self):
self.assertEqual('fizz', self.fb.fizz_buzz(3))
def test_five_gets_buzz(self):
self.assertEqual('buzz', self.fb.fizz_buzz(5))
def test_six_gets_fizz(self):
self.assertEqual('fizz', self.fb.fizz_buzz(6))
def test_fifteen_gets_fizzbuzz(self):
self.assertEqual('fizzbuzz', self.fb.fizz_buzz(15))
if __name__ == '__main__':
unittest.main()
I decided to split the file, as having the tests separate will read better.
The tree structure is
|-fizzbuzz
|-app
- __init__.py
-fizzbuzz.py
|-tests
- __init__.py
-test_fizzbuzz.py
I split the file as below (only showing part of it, one test and passing code)
#fizzbuzz.py
class FizzBuzz():
def fizz_buzz(self, number):
return str(number)
and
#test_fizzbuzz.py
import unittest
from fizzbuzz import FizzBuzz
class FizzBuzzTests(unittest.TestCase):
def setUp(self):
self.fb = FizzBuzz()
def test_one_gets_one(self):
self.assertEqual('1', self.fb.fizz_buzz(1))
if __name__ == '__main__':
unittest.main()
In the fizzbuzz directory, i run tests with python tests/test_fizzbuzz.py which should pass.
But when I comment out the return str(number) or remove it, I still pass.
If I change self.assertEqual('1', self.fb.fizz_buzz(1)) to self.assertEqual('12', self.fb.fizz_buzz(1)) it is still running the code which is not there.
I deleted all the .pyc files in the test directory and now I get ImportError: No module named fizzbuzz error
If I move the fizzbuzz.py file into the test folder and run python tests/test_fizzbuzz.py the tests pass (this is how I got the .pyc files). But this is not what i want.
Why is this? Have I split this file up wrong? Am I missing extra code or files?
If I have split it up wrongly, what would be the best way to do so?
Any help appreciated, thanks.
fizzbuzz.pyneeds to be on yourPYTHONPATH. But your use of__init__.pysuggests that you want to use packages, in which caseappis needs to be in yourPYTHONPATH, and you should importapp.fizzbuzz