Unit test — Unit Testing Framework - Sajid231/Bengali-Newspaper GitHub Wiki

Unittest — Unit Testing Framework

Unit Testing is the first level of software testing where the smallest testable parts of the software are tested to determine whether they are fit for use. It determines the quality of your code. To achieve this, unit test supports some important concepts in an object-oriented way:

Test fixture

A test fixture represents the preparation needed to perform one or more tests, and any associated cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.

Test case

A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs. Unittest provides a base class, TestCase, which may be used to create new test cases.

Test suite

A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.

Test runner

A test runner is a component that orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

Usage

  • Import unittest
  • Define setUp() function to define the initial condition
  • The name of the test function must start with test_*()
  • From terminal run command: py file_name.py

Assert Methods

Basic terms used in the code :

  • assertEqual() – This statement is used to check if the result obtained is equal to the expected result.
  • assertTrue() / assertFalse() – This statement is used to verify if a given statement is true or false.
  • assertRaises() – This statement is used to raise a specific exception. 04

Naming Convention

We will name our test files test_file.py. This naming convention will group together all of the test files. Eg. circle.py, test_circle.py.

Program:

05

Run the program:

06

How to write the Unit test:

07

Run the Unit test:

08

Test File Result

09