Curso de Python. Documentación y pruebas. Vídeo 76
Introduction to Testing with Documentation
In this section, we will learn about performing tests using documentation in Python. We will explore the use of the doctest module to write and execute tests based on the documentation.
Performing Tests with Documentation
- When programming complex applications, it is important to test if the code works correctly before putting it into production.
- The
doctestmodule allows us to perform tests based on the documentation of our functions.
- We will create a function called
triangle_areathat calculates the area of a triangle using its base and height.
- To document a function, we enclose the documentation within triple quotes before defining the function.
- To perform a test using documentation, we place three angle bracket symbols (
>>>) followed by the function name and input parameters within the documentation.
- We then specify what output should be expected from the function for those input parameters.
- By importing and running
doctest.testmod(), we can execute all tests written in our documentation.
- If all tests pass, no output is displayed. If there are errors or mismatches between expected and actual outputs, they are reported.
Writing Tests Using Documentation
In this section, we will see how to write tests using documentation. We will document our functions and specify expected outputs for different input scenarios.
Writing Tests in Documentation
- After defining a function, we can document it by enclosing its description within triple quotes.
- To write a test within the documentation, we use three angle bracket symbols (
>>>) followed by the function name and input parameters.
- Below that, we specify what output should be returned by the function for those input parameters.
- This allows us to check if our functions behave as expected without executing them separately or relying on external test cases.
- We can import the
doctestmodule and use thetestmod()function to execute all tests written in our documentation.
- If the actual outputs match the expected outputs, no output is displayed. Otherwise, errors are reported.
Handling Test Errors
In this section, we will learn how to handle test errors when using documentation for testing. We will see what happens when a function does not return the expected output.
Handling Test Errors
- When running tests using documentation, if there is an error or mismatch between expected and actual outputs, it will be reported.
- The error message includes information about which file is being examined and specifies that an example failed with certain parameters.
- It also shows the expected output specified in the documentation and the actual output returned by the function.
- Test errors indicate that there might be an issue with the function's implementation and prompt us to review and fix it.
Conclusion
In this section, we conclude our discussion on testing with documentation. We highlight that performing tests using documentation is useful for complex applications but may not be necessary for simple functions.
Conclusion
- Testing with documentation allows us to check if our functions behave as expected before running complex applications.
- By writing tests within the function's documentation, we can easily verify its correctness without separate test cases.
- However, for simple functions where behavior is straightforward, extensive testing with documentation may not be necessary.
Documentation and Function Return
In this section, the importance of documenting functions and specifying their return values is discussed. The example of a function that calculates the area of a triangle is used to illustrate the need for clear documentation.
Documenting Functions
- It is crucial to provide clear documentation for functions, including specifying what the function should return.
- In the example of calculating the area of a triangle, it is important to specify that the expected return value should be a string with single quotes.
- Failure to document the expected return value can lead to test failures and confusion.
Testing Function Return
- Multiple tests can be performed within the documentation using different parameters.
- By providing different input values, such as base and height, multiple tests can be conducted to verify if the function returns correct results.
- Each test case should have an expected result specified in quotation marks.
Performing Multiple Tests on Functions
This section explains how to perform multiple tests on functions using different input parameters. The example of a simple area calculation function is used again to demonstrate this concept.
Conducting Multiple Tests
- After performing one test on a function, additional tests can be conducted by changing input parameters.
- By modifying parameters like base and height in an area calculation function, various scenarios can be tested.
- Each test case should have its own expected result specified in quotation marks.
Handling Test Failures
- If any test fails, it is important to review why it failed with specific parameters.
- Syntax errors or incorrect quotations may cause failures in certain cases.
- While conducting multiple tests helps ensure proper functionality, it may not always be necessary for simple functions.
Example: Checking Email Validity
This section introduces an example where multiple tests are useful - checking email validity based on whether it contains an "@" symbol. The function "check_mail" is used to demonstrate this concept.
Function Description
- The function "check_mail" evaluates a received email to determine its validity based on the presence and position of the "@" symbol.
- If the email contains only one "@" symbol and it is not at the end, it is considered valid.
Implementing Multiple Tests
- Multiple tests can be conducted by providing different email inputs to the "check_mail" function.
- Examples include emails with no "@" symbol, multiple "@" symbols, or an "@" symbol at the end.
- Each test case should have its own expected result specified in quotation marks.
Code Implementation
- The code implementation involves using string functions like "count" and "len" to examine the email input.
- By counting the number of "@" symbols and evaluating their positions, the validity of the email can be determined.
The transcript provided does not cover any further sections or topics.
Function check_mail
In this section, the function check_mail is introduced. It returns true when the email is correct and false when it is not correct based on certain conditions.
Evaluating Email Conditions
- The function
check_mailevaluates multiple conditions to determine if an email address is correct.
- Tests are performed to check if the email contains an '@' symbol and if it meets other specified criteria.
- Various tests are conducted to ensure that the function works correctly for different scenarios.
Testing the Function
- Multiple tests are performed to validate the functionality of the
check_mailfunction.
- Each test checks a specific condition, such as checking if an email with an '@' symbol is considered correct.
- The expected output for each test is discussed, and it should return true or false based on the condition being evaluated.
Identifying Failures
- If there are failures in the tests, it indicates that something might be programmed incorrectly in the
check_mailfunction.
- Examples of failures include incorrect evaluation of whether an email contains an '@' symbol or returning incorrect results for certain conditions.
Importance of Doc Test
- Doc test module can be used to perform tests within documentation.
- By carefully reviewing failures reported by doc tests, programmers can identify issues in their code and make necessary corrections.
Conclusion
In this section, the importance of doc testing for identifying programming errors is emphasized. Reviewing failures reported by doc tests helps programmers improve their code's accuracy and functionality.