Unit test Flashcards
unit test
Work with existing or new tests written with the unit test module.
Understand the differences between testing types and when to apply them.
Describe some of the common challenges when testing.
unit test script
import unittest
class TestAssertions(unittest.TestCase):
def test_equals(self): self.assertEqual("one string", "one string")
if __name__ == ‘__main__’:
unittest.main()
Run tests
There are two ways to run a test file. Let’s look again at the end of the test_assertions.py file, where the unittest.main() call allows running the tests by executing the file with Python:
1-The test run is possible because of the if block at the end, where the condition is only met when running the script directly with Python. Let’s check the output when running it in that way:
2-Another way to run tests with the unittest module is by using it with the Python executable. This time, it isn’t necessary to specify the filename because tests can be discovered automatically:
naming conventions
Test discovery is only possible when you follow naming conventions for methods, classes, and files. If the file does not start with the word test, it won’t get executed.
naming conventions
The convention is that they need to be prefixed with test. Although it isn’t required, test classes use camel-casing, and test methods are lower-case, and words are separated with an underscore. For example, here’s how a test for customer accounts that verify creation and deletion could look:
class TestAccounts(unittest.TestCase):
def test_creation(self): self.assertTrue(account.create()) def test_deletion(self): self.assertTrue(account.delete())
Assertions and assert methods
note:Python data structures like dictionaries and lists evaluate as true when they have at least one item in them and false when they are empty. Avoid evaluating data structures in this way with assertTrue() and assertFalse() to prevent unexpected items from going undetected. It is preferable to be as accurate as possible when testing.
t’s essential to use assert methods instead of Python’s built-in assert() function to have rich reporting when failures happen. The test_assertion.py example uses self.assertEqual(), one of the many special methods from the unittest.TestCase class to ensure that two values are equal:
self.assertTrue(value): Ensure that value is true.
self.assertFalse(value): Ensure that value is false.
self.assertNotEqual(a, b): Check that a and b aren’t equal.
*** assert true method tests if the calculation is true or false
unit test
Integration testing
Functional testing
Integration testing
Here are some accepted norms about integration testing:
Not as fast as unit tests, and they tend to require more setup upfront before running.
Functions, methods, or classes are tested with functionality in other functions, methods, or classes.
An external service might be used, but not all the services for the application.
functional testing
Not as fast as unit tests, and they tend to require more setup upfront before running.
Functions, methods, or classes are tested with functionality in other functions, methods, or classes.
An external service might be used, but not all the services for the application.
What is one common side-effect for functions when there are no tests involved?
Functions can grow in complexity and size.
What is a recognizable aspect in functions and methods when testing is involved?
hey tend to have a single responsibility instead of doing many different things
What is the correct Python invocation to use test auto discovery?
python -m unittest