Testing and Debugging

Writing and Running Unit Tests with Unittest or Pytest

  • Unittest and Pytest are two of the most used unit testing frameworks in Python1arrow-up-right.

  • Unittest supports running Python unittest-based tests out of the box2arrow-up-right. It’s meant for leveraging existing unittest-based test suites to use pytest as a test runner and also allows to incrementally adapt the test suite to take full advantage of pytest’s features2arrow-up-right.

  • Pytest will automatically collect unittest.TestCase subclasses and their test methods in test_*.py or *_test.py files2arrow-up-right.

  • Almost all unittest features are supported: @unittest.skip style decorators; setUp/tearDown; setUpClass/tearDownClass; setUpModule/tearDownModule2arrow-up-right.

Example

# Example 1: Basic structure of unittest
import unittest

class Testing(unittest.TestCase):
    def test_string(self):
        a = 'some'
        b = 'some'
        self.assertEqual(a, b)

    def test_boolean(self):
        a = True
        b = True
        self.assertEqual(a, b)

if __name__ == '__main__':
    unittest.main()

Debugging Techniques and Tools

  1. Example of coding:

Code Profiling and Optimization

Example

Last updated