Python - Writeup - Exp1
Python - Writeup - Exp1
Python - Writeup - Exp1
Experiment No. 1
Theory:
Software Testing involves execution of software/program components using manual to evaluate one or more pro
is a technique in which particular module is tested to check by developer himself whether there are any errors. Th
Python provides the unittest module to test the unit of source code. The unittest plays an
essential role when we are writing the huge code, and it provides the facility to check whether
the output is correct or not.
Normally, we print the value and match it with the reference output or check the output
manually.
Python testing framework uses Python's built-in assert() function which tests a particular
condition. If the assertion fails, an AssertionError will be raised. The testing framework will then
identify the test as Failure. Other exceptions are treated as Error.
The following three sets of assertion functions are defined in unittest module −
Basic assert functions evaluate whether the result of an operation is True or False. All the assert
methods accept a msg argument that, if specified, is used as the error message on failure.
You can write both integration tests and unit tests in Python. To write a unit test for the built-in
function sum(), you would check the output of sum() against a known output.
For example, here’s how you check that the sum() of the numbers (1, 2, 3) equals 6:
This will not output anything on the REPL because the values are correct.
If the result from sum() is incorrect, this will fail with an AssertionError and the
message "Should be 6". Try an assertion statement again with the wrong values to see
an AssertionError:
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"
Now you have written a test case, an assertion, and an entry point (the command line). You can
now execute this at the command line:
$ python
test_sum.py
In Python, sum() accepts any iterable as its first argument. You tested with a list. Now test with a
tuple as well. Create a new file called test_sum_2.py with the following code:
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"
def test_sum_tuple():
assert sum((1, 2, 2)) == 6, "Should be 6"
When you execute test_sum_2.py, the script will give an error because the sum() of (1, 2, 2) is 5, not 6. The resul
Here you can see how a mistake in your code gives an error on the console with some
information on where the error was and what the expected result was.
Writing tests in this way is okay for a simple check, but what if more than one fails? This is
where test runners come in. The test runner is a special application designed for running tests,
checking the output, and giving you tools for debugging and diagnosing tests and applications.
Python contains many test runners. The most popular build-in Python library is
called unittest. The unittest is portable to the other frameworks. Consider the following three top
most test runners.
unittest
nose Or nose2
pytest
unittest
The unittest is built into the Python standard library since 2.1. The best thing about the unittest, it comes with bot
Example -
import unittest
class TestingSum(unittest.TestCase):
def test_sum(self):
self.assertEqual(sum([2, 3, 5]), 10, "It should be 10")
def test_sum_tuple(self):
self.assertEqual(sum((1, 3, 5)), 10, "It should be 10")
Output:
.F
-
FAIL: test_sum_tuple (__main .TestingSum)
--
Traceback (most recent call last):
File "<string>", line 11, in test_sum_tuple
AssertionError: 9 != 10 : It should be 10
FAILED (failures=1)
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/usr/lib/python3.8/unittest/main.py", line 101, in init
self.runTests()
File "/usr/lib/python3.8/unittest/main.py", line 273, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: True
As we can see in the output, it shows the dot(.) for the successful execution and F for the one
failure.
Activities:
Write a program to add multiple numbers in loop and test it by running minimum 3 test cases
Outcomes:
Questions:
References:
1. Daniel Arbuckle, Learning Python Testing, Packt Publishing, 1st Edition, 2014 Wesly J Chun, Core Pyth
2. Albert Lukaszewsk, MySQL for Python, Packt Publishing, 1st Edition, 2010 Eric Chou, Mastering Pytho
3.
4.
5.