Manual and Automated Testing in Python
Manual testing is a typeof software testing in which test cases are executed manually by a tester without using any automated tools.
The purpose of manual testing is to identify the bugs, issues, and defects in the software application.
Manual software testing is the most primitive technique of all testing types and it helps developers find critical bugs in the software applications. We shall look at the various types of testing that we have:
Types of Testing
Black Box Testing: No source code access.
White Box Testing: Source code acess.
- Unit Testing:Testing a specific unit of code [function, class, etc].
- Integration testing:Testing of combinations of units.
Sytem Testing:Testing of the complete software system.
Acceptance Testing:Testingto ensure software meets specifications requirements.
Automated Test Types in python
We shall verify and discover the different types of automated testing we can accomplish with python code.
Unit Tests
This is written by developers. The unit tests are usually written as a separate code in a different file. Pythonincludes the unittest module by defultfor writing and executing unit tests.
Integration Tests
Integration testing is the testing of multiple components of the application to check that they work together.
Integration testing might require acting like a customer or user of the application by:
- Calling an HTTP REST API
- Calling a Python API
- Calling a Web services
- Running a command line
Mocks and Stubs
Mocking
This involves creating a fake version of an external or internal service that can standin for the real one, helping your tests run more quickly and more reliably.
When your implementation interacts with an object's properties, rather than its function or behavior, a mock can be used.
Stubbing
Stubbing involves creating a stand-in, but a stub only mocks the behavior, but not the entire object.
Stubbing is used when your implementation only interacts with a certain behavior of the object.
Tests Execution
It is imperative to note that when a source code is checked into a repository, unit and integration tests shouldbe run to ensure the new code does not break working functionality. Github includes actions that can be executed to run tests when code is checked in.
How to use unittest module to write unittests in python
import unittest
def fun(x):
return x + 1
class MyTest(unittest.TestCase):
def test(self):
self.assertEqual(fun(3), 4)
We can just run the unit test by doing the following simple method:
if __name__ == '__main__':
unittest.main()
When we carefully examine the test above, we discover that the unittest module was first imported and then we define a function that returns x + 1.
We define a class MyTest which inherits the unittest module with its special testing module TestCase. We define a function test which takes in the self parameter referring to the MyTest class.The assertion assertEqual method gets called with the self parameter and the func function passed into it with 3 as an argument. The assertEqual asserts whether the x + 1 (with x as 3) equals 4. It returns true if this is so and false if otherwise.
It is important to note that the unitest written can actually be run at the same python script:
import unittest
def func(x):
return x + 1
class MyTest(unittest.TestCase):
def test(self):
self.assertEqual(func(3), 4)
if __name__ == '__main__':
unittest.main()
The test passes when we run it:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
if we decide to make the test fail, we can decide to make the function return x+2 instead of x+ 1 as below:
def func(x):
return x + 2
The test fails with an assertEqual error as shown below:
F
======================================================================
FAIL: test (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 7, in test
self.assertEqual(func(3), 4)
AssertionError: 5 != 4
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
We shall also look at the various other assertions used in python testing.
Assertions
assertEqual()
This statement is used to check if the result obtained is equal to the expected result obtained.
asserTrue()/assertFalse()
This statement is used to verify if a given statement is true or false.
assertRaises()
This statement is used to raise to raise a specific exception.
Summary
In summary, we discover that AssertEqual will fail when the resuls are not equal. AssertTrue will fail the test evaluates to false. AssertFalse will fail the test evaluates to true. The tests should be in their own module with the call to the unittest.main(). To study more about python testing,check up the documentation at [ unit test](docs.python.org/3/library/unittest.html " unittest in python").