1

I Wrote code to test that the Circle class methods circumference() and area() are returning the correct outputs. When the unit test class is run, it says that it has run 0 tests.

class Circle:
def __init__(self, r=1):
 self.radius = r
 def serRadius(self, r):
 self.radius = r
 def getRadius(self):
 return self.radius
 def area(self):
 return 3.14 * self.radius * self.radius
 def circumference(self):
 return 2 * 3.14 * self.radius
import unittest
from Circle import Circle
class TestCircleMethods(unittest.TestCase):
 def setUp(self):
 print('\nsetUP')
 circle = Circle(self, r=1)
 def tearDown(self):
 print('tearDown\n')
 def test_area(self):
 print('Test Area\n')
 assertEqual(circle.area(), 3.14)
 def test_circumference(self):
 print('Test Cirumference\n')
 assertEqual(circle.circumference(), 6.28)
 if __name__ == '__main__':
 unittest.main()
asked Apr 24, 2019 at 20:14

2 Answers 2

2

You were missing self in a few places in the tests. Also, Circle(self, r=1) should be Circle(r=1). The indentation seemed wrong too, but I'm not sure if that happened when you copy pasted the code. You can double check that too.

This works:

class Circle:
 def __init__(self, r=1): self.radius = r
 def serRadius(self, r): self.radius = r
 def getRadius(self): return self.radius
 def area(self): return 3.14 * self.radius * self.radius
 def circumference(self): return 2 * 3.14 * self.radius
import unittest
class TestCircleMethods(unittest.TestCase):
 def setUp(self):
 print('\nsetUP')
 self.circle = Circle(r=1)
 def tearDown(self):
 print('tearDown\n')
 def test_area(self):
 print('Test Area\n')
 self.assertEqual(self.circle.area(), 3.14)
 def test_circumference(self):
 print('Test Cirumference\n')
 self.assertEqual(self.circle.circumference(), 6.28)
if __name__ == '__main__':
 unittest.main()
answered Apr 24, 2019 at 20:15
Sign up to request clarification or add additional context in comments.

Comments

0

assertEqual is basically a class instance method of unittest.TestCase. So you have to call them self.assertEqual.

The first argument of any instance method is implicit, so no need to pass by yourself as you did when calling the constructor function of Circle, circle = Circle(self, r=1). It should be circle = Circle(r=1).

With all these fixes, your code to test should pass all.

ciricle.py

class Circle:
 def __init__(self, r=1):
 self.radius = r
 def setRadius(self, r):
 self.radius = r
 def getRadius(self):
 return self.radius
 def area(self):
 return 3.14 * self.radius * self.radius
 def circumference(self):
 return 2 * 3.14 * self.radius

ciricle_test.py

import unittest
from circle import Circle
class TestCircleMethods(unittest.TestCase):
 def setUp(self):
 print('\nsetUP')
 self.circle = Circle(1)
 def tearDown(self):
 print('tearDown\n')
 def test_area(self):
 print('Test Area\n')
 self.assertEqual(self.circle.area(), 3.14)
 def test_circumference(self):
 print('Test Cirumference\n')
 self.assertEqual(self.circle.circumference(), 6.28)
if __name__ == '__main__':
 unittest.main()
answered Apr 24, 2019 at 20:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.