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 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 :: what is scaling 
Python :: rstrip python3 
Python :: print column name and index python 
Python :: django select_related and prefetch_related 
Python :: how to add space in python 
Python :: sys.argv python example 
Python :: infinity range or infinity looping 
Python :: Python simple number formatting samples 
Python :: isenable selenium python 
Python :: myshop flower notimplementederror 
Python :: Class 10: Conditional Statements in Python [IF, ELIF, ELSE] 
Python :: using pickle to create binary files 
Python :: eastvale roblox python 
Python :: output multiple LaTex equations in one cell in Google Colab 
Python :: python how to extend a class 
Python :: b-spline quantile regression with statsmodels 
Python :: pycountry get 
Python :: how to get source code of website in python 
Python :: showing typle results with for loop in py in one line 
Python :: dadfa 
Python :: how to output varibles in python 
Python :: multiple channel creating command in discord.py 
Python :: python type checking dictionary mypy 
Python :: integer to binary python 16 bit 
Python :: dickyfuller test in python 
Python :: int and text on same line python 
Python :: python get function from string name 
Python :: voting classifier with different features 
Python :: crear ondas segun musica python 
Python :: url namespaces for django rest router urls 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =