Curso de Python. Documentación y pruebas II. Vídeo 77

Curso de Python. Documentación y pruebas II. Vídeo 77

Introduction to Documentation and Testing in Python

In this video, we will explore more complex tests using the doctest module. We will learn how to perform tests with nested expressions, loops, and conditional statements.

Creating a Function for Square Root Calculation

  • We create a function called square_root that takes a list of numerical values as input.
  • The function uses the sqrt method from the math module to calculate the square root of each value.
  • The function returns a new list containing the square roots of the input values.

Performing Tests with Nested Expressions

  • We want to test if our square_root function correctly calculates the square roots of given numerical values.
  • To complicate the test, we create an empty list called list.
  • Using a for loop, we add several numerical values (4, 9, 16) to the list.
  • This nested expression demonstrates how to use loops and conditional statements in doctests.

Conclusion

By using nested expressions and doctests, we can perform more complex tests on our functions. This allows us to thoroughly test different scenarios and ensure our code is functioning correctly.

Nesting Expressions in Code

In this section, the speaker discusses nesting expressions in code and demonstrates how to nest expressions using a list. The purpose of nesting expressions is to perform more complex tests and handle potential errors.

Nesting Expressions in Code

  • When nesting expressions in code, use three points (...) to indicate a nested expression.
  • By nesting expressions, we can add elements to a list within a loop.
  • The speaker demonstrates an example where the first loop adds 4 to the list, the second loop adds 9, and the third loop adds 16.
  • This allows us to have a list with values 4, 9, and 16.

Calling Functions and Returning Values

In this section, the speaker explains how to call functions and return values. They demonstrate calling the square root function on the previously created list.

Calling Functions and Returning Values

  • To call a function, use its name followed by parentheses containing any required parameters.
  • The speaker calls the square root function passing the list as a parameter.
  • The expected return values are calculated square roots of each element in the list (2.0 for 4, 3.0 for 9, and 4.0 for 16).

Testing Functions with DocTest Module

In this section, the speaker introduces testing functions using the DocTest module. They explain how to import and execute DocTests on functions.

Testing Functions with DocTest Module

  • Import the DocTest module to test functions.
  • Execute DocTests using doctest.testmod().
  • If all tests pass successfully, no output will be returned.
  • If there is an error, the module will indicate which test failed and provide an error message.

Handling Failed Tests

In this section, the speaker discusses handling failed tests and demonstrates how to modify the list to intentionally fail a test.

Handling Failed Tests

  • Modify the list used in the square root function to intentionally fail a test.
  • If a value in the list is changed, such as replacing 16 with 19, running the tests will indicate a failure.
  • The error message will show that the expected result does not match the actual result.

Testing Exception Handling

In this section, the speaker explains how to test exception handling using DocTest. They demonstrate testing for an exception when calculating square roots of negative numbers.

Testing Exception Handling

  • To test exception handling, create a new test case where one element in the list is negative.
  • When calculating square roots of negative numbers, an exception should be raised.
  • The speaker demonstrates copying code from previous tests but with a negative value in the list.
  • The first line and last line of the error message should remain consistent regardless of other lines in between.

Using Ellipsis (...) for Flexible Test Cases

In this section, the speaker explains how to use ellipsis (...) for flexible test cases. They discuss how ellipsis allows variations within certain parts of a test case.

Using Ellipsis (...) for Flexible Test Cases

  • Use three points (...) as placeholders within a test case to allow flexibility in certain parts.
  • The three points act as wildcards and can match any number of lines or characters.
  • This is useful when testing lists with varying elements or lengths.

Summary and Conclusion

In this section, the speaker summarizes the main points discussed in the video and concludes the topic of nesting expressions and testing functions.

Summary and Conclusion

  • Nesting expressions in code allows for more complex tests and handling errors.
  • Calling functions and returning values is done by using the function name followed by parentheses with parameters.
  • Testing functions can be done using the DocTest module, which checks if expected results match actual results.
  • Failed tests indicate that there is an error in the code or test case.
  • Exception handling can be tested by intentionally causing exceptions, such as calculating square roots of negative numbers.
  • Ellipsis (...) can be used as placeholders to allow flexibility in certain parts of a test case.
  • Understanding how to nest expressions and test functions is essential for writing reliable code.

New Section

This section discusses the test results and error handling in the code.

Test Results and Error Handling

  • The test is passed successfully with a negative value of -90, which returns an error.
  • When positive numbers are passed, the code no longer returns an error but instead returns the square root of the values.
  • The first line of the error message indicates that an error is expected, while the last line specifies the range of values causing the error.
  • The code provides a list with the square root of numerical values when executed.
  • Two tests are performed: one with positive values and another with negative values that should return an exception.

New Section

This section mentions future courses on Django and concludes the current course.

Future Courses on Django

  • After completing this course, there is very little left to finish.
  • The instructor will continue with another new Django course as requested by viewers.
  • Django is a Python framework that allows working with Python and offers interesting features.

Timestamps were not available for all sections.

Video description

En este vídeo vemos cómo realizar pruebas un poco más complejas utilizando expresiones anidadas con el módulo doctest. Para más cursos, ejercicios y manuales visita: www.pildorasinformaticas.es