Help me to complete unittest - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help me to complete unittest (/thread-7914.html) |
Help me to complete unittest - FooNtik - Jan-29-2018 Hello, everyone! I have made a function, which receives three integer inputs for the lengths of the sides of a triangle and returns one of four values to determine the triangle type and I need to make unittest, but nothing happends. Can someone correct my code or give advise? tri.py a = int(input("a = ")) b = int(input("b = ")) c = int(input("c = ")) def triangle_type(a,b,c): print("Enter lenghts of triangle sides:") if a<0 or b<0 or c<0: return "4=error" else: if a+b>c and a+c>b and b+c>a: if a==b==c: return "3=equilateral" elif a==b or a==c or b==c: return "2=isosceles" else: return "1=scalene" else: return "4=error" print (triangle_type(a,b,c))test_tri.py import unittest import tri class CalcTest(unittest.TestCase): #scalene def triangle_type_scalene(self): self.assertEqual(tri.triangle_type(2,3,4), "1=scalene") #isosceles def triangle_type_isosceles(self): self.assertEqual(tri.triangle_type(3,3,2), "2=isosceles") #equilateral def triangle_type_equilateral(self): self.assertEqual(tri.triangle_type(3,3,3), "3=equilateral") #error for negative sides def triangle_type_negative_sides(self): self.assertEqual(tri.triangle_type(-1,3,4), "4=error") self.assertEqual(tri.triangle_type(1,-2,3), "4=error") self.assertEqual(tri.triangle_type(1,2,-4), "4=error") #error for mismatching the condition for building triangle def triangle_type_wrong_condiion(self): self.assertEqual(tri.triangle_type(10,2,3), "4=error") self.assertEqual(tri.triangle_type(1,10,2), "4=error") self.assertEqual(tri.triangle_type(1,2,10), "4=error") RE: Help me to complete unittest - nilamo - Jan-29-2018 In the unittest module, a test will only be run if the method name literally starts with test_ . So, in your example, the first would need to be renamed to def test_triangle_type_scalene(self): .And then, after you've defined all your tests, make sure you actually run them: # define test classes if __name__ == "__main__": unittest.main() RE: Help me to complete unittest - FooNtik - Jan-30-2018 Thanks! It work! RE: Help me to complete unittest - Gribouillis - Jan-30-2018 If you want to go one step further, you can install module coverage and type in a terminal This checks that 100% of the lines of code were tested.
|