Software Testing Python
Software Testing Python
Requirements Review
Test Planning
Test Designing
Test Environment Set Up
Test Execution
Test Reporting
Levels of Testing
The three levels are:
Unit Testing
Integration Testing
System Testing
Unit Testing
Unit Testing : Testing the smallest possible code snippets of a program.
Practically, breaking down of these code snippets into further smaller pieces is
not achievable.
For e.g: considering below function as a single unit makes sense.
def square(n):
"Returns square of a number."
return n**2
Integration Testing
Integration Testing : It focuses on testing interactions across related units.
These tests are still run in isolation in order to overcome inputs from outside
or other units.
The inputs required from outside units are simulated.
System Testing
System Testing : It checks parts of the program once all units are plugged
together.
It is an extreme form of Integration Testing.
System tests will not be useful, if these tests are not supported by the results
of integration tests and unit tests.
Acceptance Testing
Unit Testing, Integration Testing and System Testing are done to verify
software product as a whole or it's individual components.
On the other hand, the testing that you perform to confirm that the software
behaves as expected is known Acceptance Testing.
Acceptance Testing doesn't check any core functionality of the software, it
only aims at checking the software behaviour.
Regression Testing
Regression Testing is another type of testing which ensures that previously
built software performs the same way after making some changes to it or
interfacing it with other software.
Regression Tests can be written before or after a bug is found.
They provide an assurance that an increase in a program's complexity doesn't
introduce new bugs.
Introduction to doctest
doctest is a python testing module, which allows writing tests based on
expected output from standard python interpreter along with documentation.
doctest is also known as "document testing" or "testable document".
doctest allows combination of tests and documentation. This feature enables
to keep documentation up to date with reality and ensures that tests express
the expected behaviour.
A doctest specifications
A doctest contains documentation and one or more valid python statements.
The statements are written after the python shell's primary prompt(>>>).
Secondary prompt (...) is used from second line on wards, when a statement
is written across multiple lines.
Expected output is written below the python statements and it should be same
as the result obtained, when you run the statement in a python shell.
===============================
Demonstrating usage of doctest
===============================
>>> 2 + 4
6
>>> -10.5 + 8
-2.5
Presence of a blank line or a new line starting with primary prompt after the
expected output is seen as end of the existing test.
Running Tests
Run the tests written in sample_tests.txt using the below command.
By default, no output is seen when all tests pass. To view a verbose output,
use -v option as shown in below command.
Output
Trying:
2 + 4
Expecting:
6
ok
Trying:
-10.5 + 8
Expecting:
-2.5
ok
1 items passed all tests:
2 tests in sample_tests.txt
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
**********************************************************************
File "D:\python 3.5\sample_module.py", line 5, in sample_module.add2num
Failed example:
add2num(-8.5, 7)
Expected:
-2.5
Got:
-1.5
**********************************************************************
1 items had failures:
1 of 2 in sample_module.add2num
***Test Failed*** 1 failures.
Expecting Exceptions
In some cases you would like to raise an exception, for example when an
invalid input is provided.
doctest can detect raised exceptions by detecting the python exception
report and traceback displayed in a python interactive shell when the
exception is raised.
doctest is concerned only with the first line Traceback (most recent call
last): and the last line, which tells it which exception you expect.
doctest reports a failure only if one of these two parts doesn't match.
Expecting Exceptions
Now add the below text, at the end of existing two tests of add2num function,
in sample_tests.txt file
Run the tests using the below command. All tests are passed.
Expecting Blanklines
The doctest considers the first blank line after the primary prompt (>>>) as the
end of expected output.
However sometimes, you may expect blank lines in the output it self. This
would mislead doctest and fail the tests.
This issue can be overcome by inserting <BLANKLINE> word instead of an
expected blank line.
The example in next slide inserts two <BLANKLINE> words in place of two
expected blank lines.