|
||||||||||
Utilization of Advanced Pytest FeaturesBy Rahul Vala, Softnautics Introduction: In the world of extreme technology advancements, each product, system or platform is completely different from another. Hence, the testing needs too vary drastically. To test a web application, the system needs are limited to just a web browser client and Selenium for test automation. Whereas an IoT product needs end device, cloud, web app, API as well as Mobile application (Android & iOS) to be tested thoroughly end to end. Similarly, there can be a completely different type of product or system. So, one size fits all is never an option when it comes to testing. To test different types of systems, one needs to be extremely versatile as well as flexible. Same goes for test automation. Each system needs a completely different type of automation framework to accommodate the complexity, scalability and different components of the product under test. If there is a single test framework which is versatile and flexible enough to achieve all of the above, it is Pytest. Pytest provides numerous advantages that can assist companies in optimizing their software testing procedures and enhancing the overall quality of their products. One of the key benefits of Pytest is its seamless integration with continuous integration and delivery (CI/CD) pipelines, which allows for automated testing of code modifications in real time. This results in improved efficiency and faster bug resolution time, ensuring that the product meets the desired level of quality and reliability. Pytest offers comprehensive reporting and analysis functionalities, which can help developers and testers promptly identify and resolve issues. Pytest offers a huge number of Features in form of Functions, Markers, Hooks, Objects, Configurations and a lot more to make the framework development extremely flexible and give freedom to the test framework architect to implement the desired structure, flow and outcome which best fits product requirement. But to make use of the above, and what to use when, is a major challenge. This blog will explain the features of different categories which are used to achieve complex automation. Hooks: Hooks are a key part of Pytest’s plugin system and are used by plugins and by Pytest itself to extend the functionality of Pytest. Hooks allow plugins to register custom code to be run at specific points during the execution of Pytest, such as before and after tests are run, or when exceptions are raised. They provide a flexible way to customize the behavior of Pytest and to extend its functionality. The categories of hooks go by the stage at which they are used. Some widely used hooks in each category summarized below: BootStrapping: At the very beginning and end of test run.
Initialization: After boot strapping to initialize the resources needed for test run.
Collection: During test collection process, used to create custom test suites and collect test items.
Runtest: Control and customize individual test run.
Reporting: Report status of test run and customize reporting of test results.
Debugging/Interaction: Interact with the test run that is in progress and debug issues.
Functions: As the name suggests, these are independent pytest functions to perform a specific operation/task. Pytest functions are directly called like a regular python function call. i.e. pytest.<function_name>(<args>) There are a number of Pytest functions available to perform different operations. Below listed are widely used Pytest functions and their uses. approx: assert that two numbers are equal to each other with some tolerance. Example: skip: skip an executing test with given message reason. Used to skip on encountering a certain condition. Example: fail: Explicitly fail an executing test with given message. Usually used to explicitly fail test while handling an exception. Example: xfail: Explicitly fail a test. Used for known bugs. Alternatively preferable to use pytest.mark.xfail. Example: skip: Skip the test with a given message. Example: raises: Validate the expected exception is raised by a particular block of code under the context manager. Example: Importorskip: Import a module or skip the test if module import fails. Example: Marks: Marks can be used to apply meta data to test functions (but not fixtures), which can then be accessed by fixtures or plugins. They are very commonly used for test parameterization, test filtering, skipping and adding other metadata. Marks are used as decorators. @pytest.mark.parametrize: Parametrization of arguments for a test function. The collection will generate multiple instances of the same test function as the number of parameters. Example: @pytest.mark.usefixtures: A very useful marker to define which fixture or fixtures to use for the underlying test function. The fixture names can be specified as a comma separated list of strings. Example: @pytest.mark.custom_markers: These are markers that are created dynamically which user can give name as per the requirement. These custom markers are mainly used to test filtering and categorizing different sets/types of tests. Example: Conclusion: Pytest is way above and over the above listed concepts. Compiled the most useful concepts that are difficult to find all at one place. These concepts are useful for a framework developer to plan the architecture of the test automation framework and make the most of them to build an efficient, flexible, robust and scalable test automation framework. About the Author Rahul Vala is working as a Principal Engineer at Softnautics and has a total of 10 years’ experience in Test Automation of different types of systems like embedded firmware, mobile and enterprise web applications. He has developed several complex Test Automation frameworks involving complex products and multiple components like boards, mobile devices, GPIO Controls, R Pi, cloud APIs, etc. He is passionate about pytest automation and loves to debug and find root cause of complex issues. In his free time, he loves to walk and play cricket and volleyball.
|
Home | Feedback | Register | Site Map |
All material on this site Copyright © 2017 Design And Reuse S.A. All rights reserved. |