Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Pyhon Learning: testing calculator classes

Shulou Source: shulou.com Published: 2022-06-03 02:48:55 09月12日 Update

The file name is: calculator.py

# calculator class

Class Count: def _ init__ (self,a,b): self.a=int (a) self.b=int (b) # addition def add (self): return self.a+self.b

The test file is: test_cal.py

From calculator import Countclass TestCount: def test_add (self): j=Count (2je 3) add=j.add () assert (5==add), 'Inter addtion result errorships' Except AssertionError as msg: print (msg) else: print ('test pass') # execute the test method; mytest=TestCount () mytest.test_add ()

Unit test: test_cal_1.py

From calculator import Countimport unittestclass TestCount (unittest.TestCase): def setup (self): print ("test start") def test_add (self): j=Count (2) self.assertEqual (j.add (), 4) def tearDown (self): print ("test end") if _ _ name__ ='_ main__': unittest.main ()

Tags: Tests files additions units methods calculators learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology MariaDB Huawei macOS