'python Unitest sample Application
I am facing problem in Python Unittesting,
simple Addition of two numbers in python Unitest, for different test case ID or Different Inputs.
-------Prasanna Yelsangikar
Solution 1:[1]
import unittest
class UnitTestExamples(unittest.TestCase):
def testsum(self):
a = 10
b = 42
self.assertEqual(a+b,52)
if __name__ == '__main__':
unittest.main()
Run the above in with your python interpretor using -v flag.
python testexample.py -v
And you will see that testsum is run and the test is exercised. unittest documentation has details how the working and the above example should be simple enough to get you started. The methods which start with the name test are run by the unittest framework.
Solution 2:[2]
#test_numbers.py is located at 'tests' folder
import unittest
class testNumbers(unittest.TestCase):
def test_add_two_numbers(self):
first_number = 3
second_number = 7
result = first_number + second_number
expected = 10
self.assertEqual(result,expected)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(testNumbers)
unittest.TextTestRunner(verbosity=3).run(suite)
run on terminal :
python -m tests.test_numbers
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Senthil Kumaran |
| Solution 2 | TheTwo |
