Please enable JavaScript.
Coggle requires JavaScript to display documents.
Testing object-oriented programs - Coggle Diagram
Testing object-oriented programs
Testing your code is important, sometimes your code can be broken without you knowing it is broken - due to the dynamic nature of Python developers believe that testing is more important in python as it is not a compiled language.
Python testing is simple and should be performed when possible! If we did not test there could be hidden errors within the code that would not be experienced until it is too late.
A full test needs to be ran rather than iterating over a single piece of code, due to the way Python works a line change can affect any other part of the program - testing sections is valid but is not a full test.
Automated testing can be written which can run parts of the program through external programs or other sections of the same program.
When designing tests to run on code it also helps to design the API, interface or pattern the code takes.
Test-driven development
Test driven development is about calling all untested code broken code - In essence this means everything should be tested as it is built.
Write tests before the code is implemented so in the future when making even a small change it can be tested to avoid hidden errors affecting other parts of the program.
Using test driven development model allows us to consider how the code will be interacted with and work on smaller chunks of the problem
The Python 'discover' module looks for modules in the folder that start with "test", once inside it will look for "TestCase" object modules and executes the tests.
We can make use of "expectedFailure()" for any tests that are expected to fail (for example still being developed or improved), doing this will mean the test will be skipped over and errors will not be present.
Unit Testing
This is the built in Python testing library that gives us a common interface for performing unit tests. Unit testing is the act of testing minimal amounts of code - the library is called "unittest"
When writing sets of tests we create a subclass of 'TestCase' then write the individual methods of testing the program - all methods must start with "test" as this is what will automate the testing.
If testing passes the method will succeed.
Due to the nature of unit testing you can have as many tests as you wish as each test will clean up after itself.
Each test should be independent of the others and be kept as short as possible.
Assertion methods
assertEqual
this is a simple test that will test equality between two parameters.
assertNotEqual
this will fail if the parameters are not equal.
assertTrue & assertFalse
is a method to check the output of an if test returns a true or false value (with assertTrue true would be outputted from success).
assertRaises
to check that a function raises a specific exception.
Using py.test
due to the simplicity and elegance of Python code py.test was created to alleviate this issue.
py.test is not part of the standard Python libraries and can easily be installed via the website or pip: "pip install pytest".
Much simpler than unittest and all properly named functions are tested, to get the results of the test you must use the "assert" statement to verify the results which leads to tests being more readable and maintainable.
py.test runs in the same folder it was executed from which in turn will search for modules and subpackages starting with "test_" / "test"