1. 程式人生 > 實用技巧 >斷言

斷言

import unittest
import random

class MyClass(object):
    @classmethod
    def sum(cls,a,b):
        return a+b
    @classmethod
    def div(cls,a,b):
        return a/b
    @classmethod
    def return_None(cls):
        return None

class MyTest(unittest.TestCase):

    def test_assertEqual(self):
        
#相等 try: a,b=1,2 sum = 13 self.assertEqual(a+b,sum) except AssertionError as e: print(e) def test_assertNotEqual(self): #不相等 try: a,b=4,2 div=4 self.assertNotEqual(a/b,div)
except AssertionError as e: print(e) def test_assertFalse(self): # try: self.assertFalse(1==2) except AssertionError as e: print(e) def test_assertTrue(self): # try: self.assertTrue(1==1) except
AssertionError as e: print(e) def test_assertIs(self): try: a=1 b=a self.assertIs(a,b) except AssertionError as e: print(e) if __name__=='__main__': unittest.main()