Fundamentos del Testing en Python
驴Qu茅 son las Pruebas Unitarias y por qu茅 es importante?
驴Qu茅 es el Testing en Software?
Instalaci贸n y Configuraci贸n del Entorno de Pruebas
Conceptos B谩sicos de Unittest
C贸mo Crear Pruebas Unitarias con UnitTest en Python
C贸mo usar el m茅todo setup en tests de Python
Uso de tearDown para limpieza de Pruebas Unitarias en Python
C贸mo validar excepciones y estructuras de datos con Unittest en Python
Control de pruebas unitarias con unittest.skip en Python
Organizaci贸n y Gesti贸n de Pruebas
C贸mo organizar y ejecutar pruebas en Python con UnitTest
Mejores pr谩cticas para organizar y nombrar pruebas en Python
T茅cnicas Avanzadas en Pruebas Unitarias
Mocking de APIs externas en Python con unittest
Uso de Side Effects en Mocking con Python
Uso de Patching para Modificar Comportamientos en Python
Exploraci贸n de Herramientas y M茅todos Complementarios
C贸mo parametrizar pruebas en Python con SubTest
Documentaci贸n de pruebas unitarias con Doctest en Python
C贸mo generar datos de prueba din谩micos con Faker en Python
Mejora y Automatizaci贸n de Pruebas
驴C贸mo asegurar la cobertura de pruebas con Coverage en Python
Automatizaci贸n de Pruebas Unitarias en Python con GitHub Actions
Pruebas Unitarias con PyTest en Python
C贸mo crear pruebas unitarias con inteligencia artificial en Python
You don't have access to this class
Keep learning! Join and start boosting your career
In this lesson, we have learned how to modify the behavior of objects and functions within our Python tests, using techniques such as patching to simulate specific situations, such as controlling the withdrawal time in a bank account. This skill is crucial when we need to validate temporal constraints or any other business logic that depends on external factors, such as time.
To implement the time restriction, we used the datetime
class to get the current time. We defined that withdrawals can only be made during office hours: between 8 AM and 5 PM. Any attempt outside these hours will throw a custom exception called WithdrawalError
.
BankAccount
class.datetime.now().hour.
Unit tests allow us to simulate different times of the day to validate that the constraints work correctly. To achieve this, we use the patch
decorator of the unittest.mock
module, which temporarily modifies the behavior of the datetime.now()
function.
patch
, we can define a specific return value for now()
, such as 7 AM or 10 AM.During implementation, we found an error in the schedule logic condition. Initially, an incorrect and
operator was used to check if the time was within the allowed range. This error was corrected by changing the condition to an or
, ensuring that the logic prohibited withdrawals before 8 AM and after 5 PM.
Contributions 8
Questions 0
Want to see more contributions, questions and answers from the community?