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

“Python unittest Framework

import unittest

class DefaultWidgetSizeTestCase(unittest.TestCase):
    def test_default_widget_size(self):
        widget = Widget('The widget')
        self.assertEqual(widget.size(), (50, 50))
Comment

PREVIOUS NEXT
Code Example
Python :: pd dataframe single column rename 
Python :: Python Tkinter Button Widget Syntax 
Python :: prolog avg of list 
Python :: opencv convert black pixels to white 
Python :: Converting categorical feature in to numerical features using target ordinary encoding 
Python :: rasperry pi camera 
Python :: load pt file 
Python :: check if the user is logged in django decorator 
Python :: python if and 
Python :: can only concatenate str (not "int") to str 
Python :: question command python 
Python :: how to replace a word in text file using python 
Python :: pandas cartesian product 
Python :: sending email with django 
Python :: How To Get Redirection URL In Python 
Python :: # write json file 
Python :: remove rows from pandas 
Python :: how to make python open an application on mac 
Python :: python logging to syslog linux 
Python :: sum of a numpy array 
Python :: lower case of string 
Python :: xpath starts-with and ends-with 
Python :: python async await run thread 
Python :: get body from request python 
Python :: save model python 
Python :: python add one month to a date 
Python :: Program to Compute LCM 
Python :: beautiful soap python get the link online 
Python :: matplotlive y axis 
Python :: python string cut last character 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =