Python Forum
Help me to complete unittest
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me to complete unittest
#1
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")
Reply
#2
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()
Reply
#3
Thanks! It work!
Reply
#4
If you want to go one step further, you can install module coverage and type in a terminal
Output:
$ python3 -m coverage run test_tri.py ..... ---------------------------------------------------------------------- Ran 5 tests in 0.001s OK $ python3 -m coverage report -m Name Stmts Miss Cover Missing ---------------------------------------- test_tri 19 0 100% tri 10 0 100% ---------------------------------------- TOTAL 29 0 100%
This checks that 100% of the lines of code were tested.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 258 Feb-25-2024, 12:51 PM
Last Post: deanhystad
Question Unwanted execution of unittest ThomasFab 9 1,949 Nov-15-2022, 05:33 PM
Last Post: snippsat
  unittest.mock for an api key silver 3 1,336 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  Ran 0 tests in 0.000s - unittest Peaches 8 4,929 Dec-31-2021, 08:58 AM
Last Post: Peaches
Sad Problem with Unittest mhanusek 1 3,680 Nov-12-2020, 04:58 PM
Last Post: Gribouillis
  Unittest et patch mad31 2 2,072 Aug-09-2020, 06:16 AM
Last Post: mad31
  Unusual things to do with unittest/HTMLTestRunner AndyHolyer 0 2,103 Jul-29-2020, 02:43 PM
Last Post: AndyHolyer
  Test a class function via "unittest " Penguin827 1 1,577 Jul-10-2020, 08:31 AM
Last Post: Gribouillis
  How to use unittest module ? binhduonggttn 1 2,007 Feb-17-2020, 03:28 AM
Last Post: Larz60+
  unittest mock and patch piscvau 1 2,071 Nov-08-2019, 03:23 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020