Skip to content

5 Unit Tests

CarlosSouza87 edited this page Jun 5, 2023 · 1 revision

🛸 Unit Tests

Unit testing is a software testing practice that focuses on testing individual units of code in isolation. A unit of code generally refers to a small, self-contained function, method, class, or component that can be tested independently of other components in the system.

Unit tests are intended to verify that each unit of code works correctly according to the defined specifications and requirements. They are designed to identify errors, failures, and unwanted behavior in units of code and ensure that those units are working as expected.

When performing unit tests, developers write specific test cases for each unit of code they want to test. These test cases cover different possible scenarios and conditions to verify that the code produces the correct results and properly handles edge cases.

1. Test file organization:

  • Test files are located in the "tests" directory of the project.
  • The test file names follow the naming pattern "test.py" or "test.py," which is compatible with the pytest test discovery pattern.

2. Usage of pytest:

  • The test structure is based on the pytest testing framework.
  • The "pytest.ini" file is used to configure pytest behavior.
  • The configuration file specifies the test file naming patterns, test directories, and other configuration options, such as filtering deprecation warnings.
  • Markers are used to categorize tests. In the provided example, two markers are defined: "unit" and "requests."

3. Usage of test libraries and modules:

  • The pytest library is imported for writing tests.
  • The unittest.mock library is imported for creating mocks and patches in tests.
  • The pytest library is also used for handling exceptions and making assertions in tests.
  • The os.path library is imported for checking file existence in the file system.
  • Other project-specific modules and classes are imported to test their functionalities.

4. Usage of fixtures and mocks:

  • The mocker.patch.object function is used to replace objects and methods during tests, allowing for the simulation of specific behaviors.
  • The unittest.mock library is used to create mock objects and define expected behaviors for method calls.
  • The mocker object is passed as an argument to test methods to provide mocking and patching capabilities.

5. Test class structure:

  • Each test class is a subclass of pytest or unittest.TestCase, depending on the testing framework used.
  • Test methods are prefixed with test_.
  • The @pytest.mark annotation is used to mark tests with specific markers.

6. Unit tests:

  • The TestAzureConnection class contains tests for the AzureConnection class.
  • The tests validate different connection scenarios with the Azure service and the expected responses.

Clone this wiki locally