Chapter 8
Answer 1
The major difference between a unit test and a functional test is the scope of testing, as described in the following:
- Unit test: A unit test usually focuses on the testing of individual components in a software that could be factored to a single function or a method of a class
- Functional test: Functional tests are also known as integration tests and usually test a specific functionality of the system that may encompass the interaction of multiple components with each other, along with their interaction with the external environment, such as database systems.
Answer 2
A test suite is a collection of test cases that needs to be run on a specific program. Writing a test suite using Python's unittest
library is quite easy to achieve. For example, if you've written a few test cases, such as TestTextInput
, TestTextUppercase
, and TestTextEncode
, we can combine them into a test suite by using the following code snippet:
import texttest # Module containing our text related test cases import unittest # Create a test loader loader = unittest.TestLoader() # Create a test suite suite = unittest.TestSuite() # Add tests to a suite suite.addTests(loader.loadTestsFromModule(texttests)
Answer 3
The purpose of fixtures inside Pytest is to provide a fixed and stable environment over which the tests cases can execute. These fixtures are responsible for initializing the environment by setting up the required variables or interfaces that may be required for a test to execute.
Another advantage of using a fixture is its reusability, which allows the same fixture to be used for multiple tests without any issue.
Answer 4
The fixture scopes in Pytest describe how often a fixture will be called. The fixtures have a lot of different scopes that can be applied to them, as shown in the following:
- Function scope: Fixture is run once per test
- Class scope: Fixture is run once per class
- Module scope: Fixture is run once per module
- Session scope: Fixture is run once per test session