Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python unittest

### file name should start with => test_ <= or end with => _test <= ###
import unittest

class TestCalculator(unittest.TestCase):
  
  @classmethod
  def setUpClass(cls): ### run once before all test cases ###
    pass
  
  @classmethod
  def tearDownClass(cls): ### run once after all test cases ###
    pass
  
  def setUp(self): ### run before each test case ###
    pass
  
  def tearDown(self): ### run after each test case ###
    pass
  
  ### make sure to add => test_ <= as prefix to all test cases otherwise they won't work ###
  def test_add(self):
        '''Test case function for addition'''
        self.calc = Calculator()
        result = self.calc.add(4, 7)
        expected = 11
        self.assertEqual(result, expected)

    def test_sub(self):
        '''Test case function for subtraction'''
        self.calc = Calculator()
        result = self.calc.sub(10, 5)
        expected = 5
        self.assertEqual(result, expected)

    @unittest.skip('Some reason')
    def test_mul(self):
        '''Test case function for multiplication'''
        self.calc = Calculator()
        result = self.calc.mul(3, 7)
        expected = 21
        self.assertEqual(result, expected)

    def test_div(self):
        '''Test case function for division'''
        self.calc = Calculator()
        result = self.calc.div(10, 2)
        expected = 4
        self.assertEqual(result, expected)
  
Comment

python unittest discover

 # discover tests recursively
  python -m unittest discover -p 'test_*.py'
Comment

PREVIOUS NEXT
Code Example
Python :: python reference parent module 
Python :: how to make text to speech in python 
Python :: size pandas dataframe 
Python :: python pandas how to get the dataframe size 
Python :: python get 2d array output as matrix 
Python :: gdscript tween 
Python :: how to import pandas in python 
Python :: create a django project 
Python :: python turn positive into negative 
Python :: python squared math function 
Python :: python slit 
Python :: virtual environments for python 
Python :: Python code to find Area of Rectangle 
Python :: Django migrations when table already exist in database 
Python :: tkinter background image python 3 
Python :: dataframe fill nan with mode 
Python :: no module named googlesearch 
Python :: seaborn distplot 
Python :: Python write value in next row of existing .text file 
Python :: delete outliers in pandas 
Python :: lambda function dataframe 
Python :: reading a file line by line using a generator 
Python :: python how to get last element in a list 
Python :: python swap numbers 
Python :: python set workdir 
Python :: return max value in list python 
Python :: how to add captcha in django forms 
Python :: python for continue 
Python :: pandas lambda applu 
Python :: pyhton image resize 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =