python Archives - testomat.io https://testomat.io/tag/python/ AI Test Management System For Automated Tests Wed, 30 Apr 2025 14:19:08 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.2 https://testomat.io/wp-content/uploads/2022/03/testomatio.png python Archives - testomat.io https://testomat.io/tag/python/ 32 32 Automation Testing Flask Application with Playwright & PyTest examples https://testomat.io/blog/automation-testing-flask-application-with-playwright-pytest-examples/ Mon, 24 Mar 2025 11:32:40 +0000 https://testomat.io/?p=19315 Let’s explore how to start implementing end-to-end testing for a web application using Python (the Pytest framework) and Playwright — an open-source tool from Microsoft. We will learn how to setup Playwright, add an E2E test for code coverage in an existing project hosted on GitHub. Before diving into the practical part, let’s explore the […]

The post Automation Testing Flask Application with Playwright & PyTest examples appeared first on testomat.io.

]]>
Let’s explore how to start implementing end-to-end testing for a web application using Python (the Pytest framework) and Playwright — an open-source tool from Microsoft. We will learn how to setup Playwright, add an E2E test for code coverage in an existing project hosted on GitHub.

Before diving into the practical part, let’s explore the basics of Flask ⬇

What Flask is and how it works?

Flask is a minimalist web framework written in Python. It includes functionality, allowing developers to extend its capabilities depending on project needs. Flask does not enforce the use of a specific database or parser by default — everything can be configured, making the framework flexible and convenient for various tasks.

Why Coding in Flask?

Key advantages of using Flask in web development:

  • Simplicity and minimalism. Flask is a microframework that includes only the essential components needed to build web applications.
  • Flexibility and Extensibility. Unlike larger frameworks like Django, it does not impose strict structures or dependencies. It supports integration with third-party libraries and extensions, making it easy to add authentication and work with database tables.
  • Built-in development server and debugging tools. It includes tools for convenient application testing and debugging. The interactive debugger helps quickly identify and fix errors.
  • Active community. Flask has a large developer community, ensuring access to extensive documentation and technical support.
  • Scalability. Despite its lightweight nature, Flask is well-suited for both small and large-scale projects.
  • RESTful API Development. Flask is widely used for building RESTful APIs, providing a simple and efficient way to manage HTTP requests.

Testing the functionality of Flask applications is a crucial step in development. Proper testing helps identify potential errors, improve code quality, and ensure system stability.

Key differences: Unit VS E2E testing for Flask application

Testing the functionality of Flask applications is a crucial step in development. Proper testing helps identify potential errors, not only related to code quality and system stability, but it also has an impact on user experience.

In Flask applications, conducting unit tests is used for verifying individual functions and routes, while the purpose of end-to-end tests is to validate the entire application flow, ensuring that all integrated components work correctly from start to finish. E2E tests are effective in simulating real user scenarios.

AQA teams commonly use these automated testing tools for Python, such as PyTest or Playwright frameworks, allowing them to detect errors promptly before the product launch.

In this tutorial, we stay focused on testing rather than API development, we will concentrate exclusively on testing the application. The application development itself, you can learn with the video Flask Tutorial for Beginners of How to build a simple meme Python website by NetworkChuck

We are considering a simple App built with Flask that retrieves data via a public API, which is the content base of the project. Based on it, we will demonstrate how to write three types of tests, API, Unit and E2E, for a Flask application. The sample of this App you can download or clone by following the link.

Once the repository is downloaded, make sure — your Python version is 3.5 or later.

Next, you can install the necessary packages by running the following command:

pip install --upgrade pip

To run this Flask application, execute the following command:

flask run
Automation testing Flask Application
Simple Flask Meme Application for testing

After that, you can see a message with localhost port where your App is running 👀

Now, when we announce the basics of Flask, it’s time to get acquainted with test framework examples and set it up for our Flask app.

First Pytest test for Flask

Pytest is a convenient Python testing framework that helps developers create higher-quality systems and confidently release them into production. With Pytest, it is easy to write both small and scalable test cases. Below you can find an example of a basic test structure using Pytest.

Pytest Project Prerequisites

First, install Pytest by executing the following command in your project’s terminal:

pip install pytest

Note: If you are working in a virtual environment, you need to activate it before installation. Using a virtual environment is the optimal solution for isolating different projects and their dependencies in Python. To know more, you can use Python Doc virtual env library

After completing the installation, connect the Pytest module. To do this, add the following line to any test file:

import pytest

Store your tests in the tests directory (folder) located in the root of the project. Pytest recommends naming test files in the format test_*.py or with the suffix **_test.py Using these standards simplifies automatic test discovery and helps avoid confusion during execution. In the screenshot below, you can see two files of tests.

Automation testing Flask application code example
First test Flask with PyTest

In this example of the first Pytest test, we check the response from the API’s default endpoint (/). The API endpoints’ response should be 200 OK. This test fragment is used to check whether we can retrieve a meme. It ensures that the homepage loads successfully and contains the expected content.

But first, to complete the installation, connect the Pytest request module:

pip install pytest requests

Execute the test using pytest:

pytest ./tests

You can also run a specific test file by explicitly specifying its name after the Pytest command:

pytest test_app.py

After executing these commands, Pytest will automatically find all tests in the root directory or the specified file of a standard naming pattern, so you don’t need to specify file or directory names manually.

To provide further verification, we can include a content assertion validation to see if the response matches our expectations, if the client response contains the <h1>title phrase ‘Just Chill and enjoy some memes!’ and is a meme present in the <img> and tag is not empty. Therefore, we have adjusted our test accordingly. This test is a kind of Unit test — the full test_app.py file:

import requests

# Define the base URL globally to avoid repetition in each test
BASE_URL = 'http://127.0.0.1:5000/'

def test_homepage():

    # Send a GET request to the homepage
    response = requests.get(BASE_URL)

    # Assert that the request was successful (status code 200)
    assert response.status_code == 200


def test_homepage_title():

    response = requests.get(BASE_URL)

    # For example, if the homepage includes a specific title or text
    assert b'<h1>Just Chill and enjoy some memes!</h1>' in response.content
    assert b'Infinite Memes' in response.content

def test_homepage_img_meme():

    response = requests.get(BASE_URL)

    # Check if the <img> tag is present in the response content
    img_tag_start = response.text.find('<img src="')
    assert img_tag_start != -1, "No <img> tag found on the page"

    # Extract the URL from the <img> tag's src attribute
    img_tag_end = response.text.find('"', img_tag_start + len('<img src="'))
    meme_pic = response.text[img_tag_start + len('<img src="'):img_tag_end]

    # Assert that meme_pic is not empty
    assert meme_pic != '', "meme_pic should not be empty"

We have received confirmation that our test has been successfully executed! Pytest marks a successfully executed test with a green dot, and a failed test with a red letter F.

Note: If you are debugging tests through the console and want to output the response, you can use the command $ pytest -s test_*.py to write to stdout. By using the s option, you will be able to output console messages inside the tests and debug them while they are running.

When new features are added to the application, the number of tests should be increased to ensure everything works correctly. Pytest allows you to run multiple tests from one file, grouping them together. It also offers markers that allow you to assign attributes and characteristics to test functions.

Web App Testing with Playwright, Pytest Alternative

Choosing the appropriate testing framework is crucial as it ensures an efficient and seamless verification process, helping to identify and fix issues early in the development stage.

Check out the comparison table we prepared for you ⬇

When to Use Playwright When to Use Pytest
E2E, UI Testing Playwright is ideal for automated end-to-end UI testing. It interacts with the web pages simulating user clicks, form submissions, and navigation. Pytest is not directly used for E2E testing, it can be done by integrating with other tools.
Functional Testing Ensures that user flows like real user interaction. Pytest is perfect for testing individual functions, routes, models, and database interactions of your Flask app. You can use Pytest along with Flask’s test client to directly test your app’s logic.
Cross-Browser Testing Compatible for cross-browser testing. Pytest doesn’t handle browser interactions
API Testing Playwright has built-in support for interacting with APIs, making it easy to send requests and verify responses. Pytest is ideal for testing APIs when you want to mock requests, test logic, and check assertions without browser interaction.
Unit Testing It isn’t intended for unit tests, but designed for integration tests and end-to-end testing. It is excellent for unit testing functions, classes, and modules in isolation without any UI interaction.
Performance Testing Might be a useful testing tool for performance testing by simulating real user behavior and measuring the interaction time. Not suited for performance testing; it is mainly used for functional and unit tests. Use tools like Locust or JMeter for performance testing.
Database Testing Does not have direct support for testing databases. However, it can indirectly test how data is displayed in the UI. Pytest, with appropriate fixtures, can easily test database interaction, ensure records are created, modified, or deleted properly through the Flask app.
CI\CD Integrated in CI pipelines. Used in CI pipelines.
Mocking and Stubbing Playwright can mock network requests, intercept responses, and simulate network conditions. Pytest allows mocking and stubbing with plugins only, but it’s better for testing isolated components.
Reporting Provides built-in reporting for test results, including screenshots and videos for debugging failures. Pytest offers test reporting within plugins, including HTML, JUnit, and others, to integrate with CI tools.
Error Handling Can indirectly test how errors (like 404 or 500 pages) are handled by interacting with the app’s UI and checking error page displays. Pytest is more effective for directly testing error handling, ensuring that your Flask app returns the correct error codes, error messages, and status codes for specific edge cases.

Summary:

Pytest is an optimal solution for Python testing when you need a flexible and concise approach to writing tests. This tool is renowned for its versatility and supports various types of testing, including unit, functional, and API testing, as we can see just on top. It is especially helpful for writing clean, maintainable, and highly organised test code.

Playwright is a powerful tool for conducting E2E tests and browser automation, which can be very useful when testing Flask applications that have user interfaces, such as web pages; anyway, Playwright is also quite popular for API testing. Use Playwright when you need to simulate real user interactions on a web application in a browser.

Here are a few cases when Playwright should be used for testing Flask:

✅ Testing user interaction.
✅ Testing across different browsers.
✅ Testing applications that heavily use JavaScript.
✅ Simulating real-life scenarios.
✅ Testing API with an interface.
✅ Full application testing.
✅ Testing interface reactions and error handling.

At the same time, Playwright is not suitable for unit testing. For checking isolated components or functions in Flask, it’s better to use clear Pytest, as well as for testing APIs without interface interaction.

For maximum effectiveness, use Playwright for browser automation and Pytest to manage the test execution, structure, and reporting.

Setting up Playwright for Flask

Prerequisites

First, let’s install Playwright. Playwright recommends using the official Playwright Pytest plugin to write E2E and API tests. This command will install Pytest-Playwright along with the Playwright command-line tool:

pip install pytest-playwright

However, it uses the synchronous version of Playwright. If you need to work with the asynchronous version, you can install Playwright without the Pytest plugin using the following command:

pip install playwright

The next important step is downloading the browsers required for Playwright automation to work. Run the command:

playwright install

Now, move on to the next stage – creating the first test.

Writing Your First Tests with Playwright

First, let’s create a file called first_playwright_test.py and add our first test scenario with the following code to it:

Automation test Flask application with Playwright code example
First test Flask Application with Playwright

You can run the Playwright first test for our Flask example application by following the command:

pytest first_playwright_test.py

This test is quite simple, but it already allows us to see how Playwright works. For example, we open our URL, but the test expects <title> Infinite meme, we got the error, as in truth it is Infinite Memes. If rewrite our testing framework in Playwright it looks like below:

import re
import requests
from playwright.sync_api import Page, expect

BASE_URL = "http://127.0.0.1:5000/"

def test_homepage_title_contains_infinite_memes(page: Page):
    page.goto(BASE_URL)
    expect(page).to_have_title(re.compile("Infinite Memes", re.IGNORECASE))

def test_homepage_has_main_heading(page: Page):
    page.goto(BASE_URL)
    expect(page.get_by_role("heading", name="Just Chill and enjoy some memes!")).to_be_visible()

def test_homepage_has_image_displayed(page: Page):
    page.goto(BASE_URL)
    meme_img = page.locator("img")
    expect(meme_img).to_be_visible()
    assert meme_img.get_attribute("src") != "", "Expected meme image src not to be empty"

def test_homepage_refresh_notice(page: Page):
    page.goto(BASE_URL)
    expect(page.locator("text=auto-refresh in 15 seconds")).to_be_visible()

# ✅ API test: verify the homepage returns HTTP 200 OK
def test_api_homepage_status():
    response = requests.get(BASE_URL)
    assert response.status_code == 200, f"Expected status 200, got {response.status_code}"

Running Playwright test for Flask Application with Codegen

Playwright has an automatic code generator — Codegen. This command opens a browser window and Playwright inspector for the specified URL, allowing you to track actions.

playwright codegen http://127.0.0.1:5000/

We will use Codegen Playwright’s graphical interface to check our simple Flask web application and gradually add more tests by performing interactions on the page.

Playwright Flask Codegen Generation

After the page loads, you can navigate across the page, and all actions with conditions will be recorded automatically. Just select the Record. The inspector window shows how the code reacts to our actions on the page. We can then experiment with the behavior we are interested in.

Codegen attempts to create reliable text-based selectors. Some results might not be optimal, for example, too generic. Thus, we made some fixes in the generated code:

 Flask App with Playwright screen
Test Generated by Playwright Codegen for Flask App

In our case, we want to precisely target elements by their existing on the page, by text. This example ensures that even minor changes in the interface will be detected during testing.

Although, as you know, there are unit tests to verify this at the code level, the ability to simulate a user action — registering and uploading a file — allows detecting issues at all levels of the application.

Importance of Presenting Test Results, Reporting

Real-time reporting helps track testing efforts. It detects defects. Gives insights into which areas are okay and which need more attention. This is obvious for release planning, especially within Agile teams. When stakeholders see transparent, detailed, and accurate test results, they can improve their testing strategies and processes. In turn, it increases their confidence in the software’s quality.

Test management system testomat.io synchronises automated and manual tests for both Platwright and Pytest frameworks in one place to see the common picture in informative reports and analytics.

Flask testing Automation source code
Real-time report displaying Playwright test results integrated with Bitbucket CI/CD pipeline, showing test execution status and metrics.
Real Time Report Playwright tests with Bitbucket CI\CD

In addition, find Flask’s built-in capabilities useful:

Reliable error handling is an important aspect of Flask application development. In Flask, exceptions can be effectively managed using try-except blocks, preventing crashes and providing users with understandable error messages. For instance, if an API request fails or invalid data is entered, exception handling will help form a helpful response instead of causing a crash. This improves the user experience and simplifies problem diagnosis.

For further analysis and troubleshooting, logging is advisable. In Flask, logging libraries can be used to record errors, making it significantly easier to track faults by their ID. Custom error pages can also be configured to display clear messages, such as for 404 errors or internal server failures.

Another useful tool is Flask’s built-in debugger, which provides detailed error traces in development mode, helping to quickly pinpoint their causes. It is essential to remember that in a production environment, debugging mode should be turned off to prevent sensitive information leaks. Using these methods enhances the stability of Flask applications and makes them more user-friendly.

Implementing logging in Flask applications is crucial for effective debugging and performance analysis. By using Python’s built-in logging module, various log levels can be captured, from debugging and informational messages to warnings and critical errors. This allows you to record events in the application, which helps quickly identify causes of failures and track user behavior.

To set up logging in Flask, the log level and record format should be defined early in the application’s setup. Logs can be stored in files for long-term analysis or output to the console for real-time monitoring during development. It is important to include additional contextual information in logs, such as timestamps and user actions. Well-integrated logging simplifies problem detection and improves application stability, ensuring its reliable operation.

Conclusion

Mastering testing and debugging techniques is key for every developer working with Flask applications. Implementing effective testing approaches, creating an appropriate environment, and using reliable diagnostic methods can significantly improve the stability and performance of products. It is important to remember that a thoroughly tested application not only ensures a better user experience but also simplifies its ongoing maintenance and development.

Playwright is a powerful tool for reliable E2E testing, and if you are working with Python, it is definitely worth considering. By integrating Playwright with Flask testing, you ensure the seamless operation of both the backend and frontend of your application, providing users with a reliable and user-friendly experience.

The post Automation Testing Flask Application with Playwright & PyTest examples appeared first on testomat.io.

]]>
Popular Python Libraries Make Python Testing More Efficient https://testomat.io/blog/popular-python-libraries-to-make-python-testing-more-efficient/ Fri, 23 Aug 2024 01:41:42 +0000 https://testomat.io/?p=15641 A Python library is a collection of pre-written code modules, classes, and functions that Python developers can use in different projects without the need for additional coding. These libraries are applied in software development, performing mathematical operations, data manipulation, automating complex tasks, and other purposes. Python can be utilized in a wide range of fields, […]

The post Popular Python Libraries <br> Make Python Testing More Efficient appeared first on testomat.io.

]]>
A Python library is a collection of pre-written code modules, classes, and functions that Python developers can use in different projects without the need for additional coding. These libraries are applied in software development, performing mathematical operations, data manipulation, automating complex tasks, and other purposes.

Python can be utilized in a wide range of fields, including:
  • data science
  • machine learning
  • deep learning
  • computer vision
  • big data
  • deep neural networks
  • Natural Language Processing (NLP), etc.

As of the time this material was prepared, the Python ecosystem included over 137,000 libraries (you can check statistics of downloads, popular repos etc) Moreover, the number of libraries is constantly growing. This is one of the reasons for the language’s immense popularity among programmers.

Advantages of Using Python Libraries for Testers

However, it’s not only Python developers who can benefit from the full range of possibilities that using libraries in Python projects offers. QA teams also gain significant advantages from their application. Here are some of them:

  • Increased efficiency and speed of testing. Thanks to libraries like Pytest or Selenium, testers can automate repetitive test cases, significantly saving time resources. The simplicity and readability of the programming language also contribute to speeding up processes, allowing QA engineers to quickly write test scripts and make changes to them.
  • Applicability for different types of testing. There are different libraries for all testing activities on a project. For example, libraries for web testing (Selenium), unit testing (PyUnit, PyTest), API testing (Requests), performance testing (Locust), and other libraries.
  • Extensive integration capabilities. Python libraries easily integrate with popular CI/CD tools and frameworks. This significantly simplifies testing in various environments.
  • Informative data analysis of test results. Some libraries, such as Pandas or NumPy, can be used for data visualization and efficient data manipulation. This allows for a deep analysis of test results and the creation of detailed reports.
  • Versatility and ease of use. Python is a cross-platform programming language. For testers, this means that all scripts can run independently of the operating systems being used, without the need for modification. This ensures flexibility in testing environments. Python libraries provide QA engineers with extensive capabilities for automation, integration, and optimization of testing processes. This makes them indispensable for effective quality control of software products.

Essential Python Libraries for Testers

We present to you a list of the best Python libraries used across various industries by Python developers and QA specialists.

PyUnit/UnitTest

PyUnit, also known as UnitTest, is a part of the Python standard library, an integrated framework used for writing and running unit tests. It offers users a wide range of features, including test automation and code sharing.

Key Features

  1. Writing Test Cases: PyUnit’s approach to writing test cases involves creating subclasses that implement test methods. This method allows for the creation of structured, logical, and organized tests.
  2. Checking Test Results Against Expectations: The framework uses assert*() methods for this purpose. For example, the assertEqual(a, b) method checks whether the statement a = b is true.
  3. Grouping Test Cases: PyUnit allows for grouping related test cases into test suites, enabling their collective execution.
  4. Setting Up and Tearing Down Test Environments: The framework supports setUp() and tearDown() methods to prepare the test environment before testing begins and clean it up afterward.
  5. Test Discovery: This feature allows for discovering and running all test cases in a specified Python module or directory. This is particularly useful for managing large-scale projects, as it saves time in handling numerous test cases.
  6. Test Reports: The framework provides detailed reports on test results, including information on failures and errors, which can be saved in a convenient and readable HTML format.

Pros

  • Built-in library – no additional installations required.
  • Reliable documentation for users.
  • User-friendly due to clear syntax and structure.
  • Extensive integration capabilities.
  • Popular among users with strong support from the Python community.

Cons

  • Less flexible compared to other Python libraries, such as PyTest.
  • Verbose test code due to detailed syntax.
  • Not very intuitive for beginners.
  • Limited features – for example, it does not support parameterized tests or built-in mocking.

PyTest

PyTest is a popular Python testing framework, alongside UnitTest, that allows users to write readable test cases for testing various scales, from simple unit tests to complex functional tests.

Key Features

Creating Test Cases with Minimal Boilerplate Code: Test functions in PyTest are simple Python functions, making them easy for testers to use.

  1. Parallel Testing: The framework offers specialized plugins (e.g., pytest-xdist) that enable parallel test execution, speeding up the testing process.
  2. Setting Up and Tearing Down Test Environments: This is possible with PyTest fixtures, which can be shared across multiple tests or even test files.
  3. Parameterized Test Functions: Users can utilize the @pytest.mark.parametrize decorator to run test functions with different parameters, avoiding code duplication.
  4. Self-Analysis of Assertions: This feature provides detailed information on failed assertions.
  5. Test Discovery: PyTest supports automatic discovery of test files and functions based on their naming conventions.
  6. Selective Test Execution: Users can tag tests with various markers — such as slow or skipped — and run each group of tests separately.

Pros

  • Simple syntax and minimal boilerplate code for ease of use.
  • Good integration with other frameworks, CI/CD tools, etc.
  • Wide range of plugins for extending functionality.
  • Detailed reporting for easy problem diagnosis.
  • Scalability and flexibility, allowing for various types of testing.

Cons

  • Dependency on plugins, which can cause compatibility issues.
  • Complex configuration, especially noticeable in large projects.
  • Steep learning curve due to some specific functionalities, such as fixtures or custom plugins.

Requests: HTTP for Humans

Requests is a powerful and user-friendly Python library for handling HTTP requests. Users don’t need to test API manually if with query strings to URLs — this tool automates all processes.

Key Features

  1. Sending HTTP Requests and Handling Responses: Requests makes this easy with a simple, intuitive API.
  2. Support for All Popular HTTP Methods: The library supports common HTTP methods such as GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH.
  3. Session Creation and Management: Users can use persistent settings and cookies for multiple requests.
  4. Content Decoding: Requests automatically decodes content using the Content-Type response header, simplifying response data handling.
  5. HTTP Error Handling: The library includes built-in exceptions like requests.exceptions.RequestException, requests.exceptions.HTTPError, and others.
  6. Keep-Alive and Connection Pooling: These features manage connections, enhancing performance and optimizing resource use.

Pros

  • Ease of use due to an intuitive API and readable code.
  • Reliable handling of various HTTP communications.
  • Excellent user support thanks to extensive documentation and an active community.

Cons

  • For streaming data transfer, you might need to use other popular libraries.
  • Performance may lag behind some lower-level libraries.
  • Asynchronous operations are not supported; for this purpose, libraries like httpx or aiohttp will be needed.

FastAPI

FastAPI is a high-performance Python framework designed for building web APIs. It features a user-friendly interface, a focus on speed, and minimal user effort.

Key Features

  1. Autocomplete Functionality: FastAPI includes built-in support for editors. Users can enter a few characters, and the framework will suggest several completion options.
  2. Automatic Interactive API Documentation: Using tools like Swagger UI and ReDoc to generate documentation simplifies testing and documenting Python APIs.
  3. Asynchronous Request Handling: The framework supports asynchronous programming with async and await, achieving high performance and efficiency in input/output operations.
  4. Built-in Dependency Injection System: FastAPI supports creating and automatically handling dependency hierarchies between endpoints.
  5. Support for Unit Testing: The framework allows writing unit tests for APIs using the TestClient class.

Pros

  • Based on standard Python type hints – no need to learn additional syntax.
  • High performance.
  • Utilizes best practices in Python for API creation and testing.
  • Community support and a growing ecosystem of plugins.

Cons

  • Relatively new framework, which is reflected in the number of available plugins.
  • Limited built-in features.
  • Not ideal for simple applications.

Selenium

Selenium is a framework for automating web browsers that is widely used for testing web applications. It consists of three tools: Selenium WebDriver, Selenium IDE, and Selenium Grid. In the context of this article, we are interested in Selenium WebDriver — a library-based tool for various programming languages, including Python.

Key Features

Interacting with Web Elements via the WebDriver API: The framework provides a comprehensive API for clicking buttons, navigating between web pages, filling out forms, and interacting with other elements.

  1. Finding Web Elements: The tool supports several search strategies using various locators, including ID, name, class name, CSS selectors, XPath, and link text.
  2. Parallel Testing: With support from Selenium Grid, QA specialists can run tests in parallel on different machines and browsers.
  3. Support for Design Patterns: The tool supports popular design patterns, such as the Page Object Model (POM), which helps create structured and maintainable test code.
  4. Running Browsers in Headless Mode: This efficient method speeds up testing by allowing the QA team to run tests in a browser without graphical user interfaces.

Pros

  • Support for various browsers, including Chrome, Firefox, Safari, Edge, and IE.
  • Easy integration with CI/CD tools and various testing frameworks.
  • High testing speed.
  • Wide range of third-party plugins to extend functionality.

Cons

  • Requires strong programming skills – the framework does not support no-code automation.
  • Limited to web application use cases.
  • Lack of built-in reporting – requires the use of third-party tools.
  • Python Locust

Python Locust

Locust is a load testing tool that uses the Python programming language for writing tests.

Key Features

  1. Writing Test Scenarios in Python: With its clear syntax, QA engineers can easily create complex user behavior simulations.
  2. Distributing Tests Across Multiple Machines: This allows for modeling the load created by millions of simultaneous users.
  3. Real-Time Monitoring: The framework provides the ability to monitor all metrics in real time. For example, testers can see request and failure rates, control response times, and more.
  4. Defining User Behavior and Scenarios: Locust includes classes and methods in Python that are especially useful for creating complex test scenarios.
  5. Built-in Reporting and Analytics: Testers can collect and analyze performance data on the tested application using Python code. The framework provides customizable metrics and reports.

Pros

  • No need for user interfaces – creating custom loads requires just a few lines of code.
  • Easy integration with third-party tools and systems, such as CI/CD pipelines.
  • Good scalability with support for distributed testing.
  • Ability to test various types of web services with support for HTTP and WebSocket.

Cons

  • Limited reporting capabilities compared to other frameworks – extended reporting requires third-party tools.
  • Support for a limited number of protocols – additional tools are needed for testing other protocols beyond HTTP and WebSocket.
  • Community and user support are less extensive than more popular tools.

Gauge

Gauge is a testing automation environment that supports multiple programming languages, including Python. Its key feature is that it allows creating test cases in a readable format independent of the language, and then automating tests using Python libraries or libraries for other languages.

Key Features

  1. Modular Approach to Writing Tests: The tool promotes code reuse by allowing the creation of reusable test steps and scenarios.
  2. Parameterization of Test Cases: The QA team can run tests with different sets of data, improving flexibility and test coverage.
  3. Effective Test Organization and Management: Gauge uses filters and a tagging system to organize and manage testing. Users can run subsets of tests filtered by specific tags.
  4. Interactive Debugging: The tool includes an interactive debugging feature that helps easily identify issues in test scenarios.

Pros

  • Tests are written in a format that is understandable even for non-technical users.
  • Code reusability speeds up the creation of test scenarios.
  • Rich ecosystem of plugins.
  • Ability to involve team members and stakeholders with no technical knowledge in testing.

Cons

  • Additional plugins and extensions are required for integration with third-party tools.
  • Steep learning curve due to creating tests in Gauge’s specific syntax.
  • Limited built-in reporting capabilities.

Python Robot Framework

Robot is a test automation framework known for its clear syntax and support for extension via different libraries, including Python. Its open-source nature and strong community support make it popular among IT professionals worldwide.

Key Features

  1. Support for Action Word-Based Testing: The tool allows for creating readable and maintainable Python tests using keywords, which can be built-in, custom, or sourced from a Python library.
  2. Organization of Test Cases: Users of the Robot Framework can write tests in tabular format, making them easy to read and manage.
  3. Data-Driven Testing Support: Parameterization of test cases is available, allowing the same tests to be run with different sets of data, which speeds up the testing process.
  4. Extensive Documentation: Robot provides detailed reports and logs, which helps in analyzing test results, identifying, and addressing issues promptly.

Pros

  • Clear syntax due to keywords and tabular format.
  • Rich Ecosystem of plugins.
  • Extensive integration capabilities with CI/CD pipelines and testing tools.
  • Strong community support and high-quality documentation.

Cons

  • Limited functionality — may not be sufficient for complex testing tasks.
  • Complexity in configuration when using third-party libraries and plugins.
  • Complex acceptance testing scenarios may require additional scripts and libraries.

Python Playwright

Playwright is a tool used for browser automation and end-to-end testing. It provides extensive capabilities for automating web applications, whether tasks are performed synchronously or asynchronously.

Key Features

  • Context Isolation for End-to-End Testing: Python Playwright achieves this by running contexts across multiple browser configurations.
  • Cross-Browser Testing Support: The tool supports all major engines, including Chromium, WebKit, and Firefox.
  • Running Tests in Headless and Headful Modes: Playwright is effective for both running tests in CI/CD pipelines and for debugging.
  • Automated Interaction with Web Pages: Clicking buttons, navigation, dragging, and other actions are possible through a user-friendly API.
  • Test Result Visualization: Users can access screenshots and video recordings of browser interactions, aiding in test report analysis.
  • Parallel Test Execution Support: Testing is significantly accelerated by running tests in multiple browser instances simultaneously.

Pros

  • High performance and reliability.
  • Well-designed API that allows the tool to handle complex testing tasks.
  • Effective debugging tools.
  • High-quality documentation.

Cons

  • Parallel testing and browser interactions can be resource-intensive.
  • Steep learning curve.
  • Less community support compared to tools that have been on the market longer.
  • Behave Framework

Behave Framework

Behave is a framework oriented towards behavior-driven development (BDD) that allows writing tests in Gherkin syntax for Python projects. By using natural language, it encourages collaboration among all team members and stakeholders.

Key Features

  1. Creating Test Cases in Natural Language Syntax: This makes tests readable even to non-technical users.
  2. Storing Tests in Feature Files: These files contain scenarios and examples of expected system behavior under various conditions.
  3. Access to Living Documentation: The feature files serve as living project documentation, updated as the application is developed and tested. They remain current and aligned with the latest requirements.
  4. Reusing Test Steps: Test scenarios can reuse steps, promoting consistency and reducing code duplication.
  5. Organizing Test Scenarios: The tool supports a tagging system, allowing you to select features or scenarios by their assigned tags.

Pros

  • Easy integration with CI/CD tools and test automation systems.
  • Improved communication within the team.
  • Access to up-to-date documentation in a human-readable format.

Cons

  • Steep learning curve for specialists new to BDD.
  • Potentially cumbersome function files when testing complex systems.
  • Excessive focus on testing at the expense of other development tasks.

Testify

Testify is a Python testing framework used as an alternative to the standard Python package’s UnitTest and Nose. This tool is quite versatile, allowing the execution of various types of tests, including unit, integration, and system tests. It can not only serve as a full replacement for these frameworks but also significantly extend their functionality.

Key Features

  1. Extended Assertions: Provides a broader range of assertions compared to UnitTest, including detailed error messages.
  2. Flexible Test Environment Configuration: Enabled by advanced fixture management.
  3. Automatic Test Discovery: Allows for faster test identification and execution without manual specification of test cases.
  4. Test Parameterization: Supports running the same test function with different sets of input data.
  5. Structured Organization of Test Cases: Allows grouping tests into classes and modules.

Pros

  • Built-in test reporting.
  • Compatible with many Python tools and libraries.
  • Excellent flexibility and multifunctionality.
  • High-quality reporting.

Cons

  • Potential issues with parallel test execution.
  • Using Testify adds an extra dependency to the project, which may be unnecessary if UnitTest’s functionality is sufficient.
  • Smaller community and plugin ecosystem compared to more popular frameworks.

Nose2

Nose2 is another Python testing framework used by QA teams to extend the capabilities of the Python module UnitTest. It is a new version of the outdated Nose framework and offers enhanced functionality for executing tests.

Key Features

  1. Automatic Test Discovery: The tool searches for and loads tests from all Python modules starting with test. Users can also run specific modules, classes, or tests by specifying their names in the command line.
  2. Support for Test Fixtures: Allows configuration and teardown of test environments.
  3. Test Parameterization: Enables running the same test with different sets of test data.
  4. Flexibility and Customization: Provides customization through the command line and configuration files, offering flexibility in test management.
  5. Detailed Reporting and Analytics: Offers detailed test results and allows filtering, enabling teams to focus on specific test outcomes.

Pros

  • Plugin architecture support, allowing for framework extensibility.
  • Compatibility with test examples created in the popular UnitTest.
  • Enhanced test discovery functionality.
  • Advanced reporting capabilities.

Cons

  • Less frequent updates compared to popular frameworks.
  • Smaller plugin ecosystem compared to UnitTest or PyTest tools.
  • Community support lags behind more popular frameworks.

Each of the described tools has its strengths and weaknesses, with different capabilities and functionalities. When choosing one, consider your project’s needs to achieve the best results.

Tools for Analyzing Python Code

In addition, software engineers Devs and QA engineers use specialized tools to write optimized Python code. Let’s explore the capabilities of some of the most sought-after tools.

Pylint

Pylint is a tool widely used for static analysis of Python code. Its primary tasks include checking code compliance with coding standards and providing suggestions for improving code quality.

Key Features

  1. Code Quality Checking: The tool evaluates how well your source code adheres to various standards, including PEP 8.
  2. Customizable Settings: The tool can be configured according to project needs. You can enable or disable checks, set their strictness, and more.
  3. Detection of a Wide Range of Errors: Pylint can identify syntax errors, code duplication, undefined variables, and more.
  4. Built-in Reporting: It provides built-in metrics and reports containing information about code quality.
  5. Refactoring Suggestions: The tool offers recommendations for fixing errors and writing clean, readable code.

Pros

  • Improved Python code quality.
  • Flexibility and customizability.
  • Easy integration with development tools and CI/CD.

Cons

  • Potentially inaccurate results, including false positives, requiring manual verification.
  • Possible configuration challenges depending on the team’s coding standards.

Flake8

Flake8 is another Python code analyzer that provides a comprehensive solution by combining the capabilities of several other tools: Pyflakes, pycodestyle, and McCabe.

Key Features

  1. Code Quality Compliance: The tool checks whether code adheres to PEP 8 standards.
  2. Custom Code Style: Allows for applying a personalized coding style through configuration files.
  3. Built-in Reporting: Provides reports that highlight existing issues and areas for improvement.
  4. Command Line Interface: Facilitates running checks, specifying files and directories, and configuring necessary parameters.

Pros

  • Versatility – Flake8 is used for detecting code errors, measuring code complexity, and checking for style violations.
  • Enhanced functionality through plugins.
  • Extensive integration capabilities with development environments and CI/CD pipelines.
  • Extensive community support.

Cons

  • Potential for false positives and false negatives.
  • Possible configuration challenges depending on the scale of the project.
  • Performance issues when working with large codebases.

Ruff

Ruff is a tool used for formatting Python code, distinguished by its high-speed checks. It serves as an excellent alternative to Flake8 and similar analyzers due to its performance and extended capabilities.

Key Features

  1. Built-in Caching: This feature prevents re-checking files that have not been modified.
  2. Automatic Error Correction: For example, the tool can automatically remove unused imports, simplifying the creation of high-quality code.
  3. Support for a Wide Range of Checks: This linter can check style, code complexity, and identify potential errors.
  4. Coding Standards Compliance: Like other code analyzers, Ruff checks if your code adheres to PEP 8 standards.
  5. Customizable Settings: The tool allows enabling or disabling various rules to meet project requirements.
  6. Support for Rules from Other Tools: Ruff includes rules from Pyflakes, pycodestyle, McCabe, and Flake8 for error detection, complexity measurement, and style checking.

Pros

  • High speed of code analysis.
  • Flexibility and ability to customize for specific projects.
  • Compatibility with other linters, development environments, and CI/CD tools.

Cons

  • Limited stability compared to more mature tools.
  • Potential for false results, requiring manual review and precise configuration.
  • Less detailed documentation compared to many popular linters.

Documentation Generators for Python

Another group of tools essential for Python development and testing is documentation generators.

MkDocs

MkDocs – a static site generator used to create project documentation from Markdown files. It can be particularly useful in the context of Python testing, as it allows for documenting QA processes and their outcomes.

Key Features

  1. Documenting Testing Strategies and Frameworks: MkDocs enables documentation of testing strategies used in a project. This may include types of tests, testing goals, their role in the development process, and more. It also allows for documenting frameworks used for Python testing, such as PyTest, UnitTest, and others.
  2. Overview of Test Suites and Test Cases: MkDocs can be used to create a comprehensive list of test modules and detailed descriptions of test cases. This helps QA team members quickly find available tests, understand their goals, expected results, and configuration details.
  3. Documenting Integration Processes: The static generator can be used to document detailed instructions for setting up CI/CD pipelines.
  4. Tracking Testing Results: Although MkDocs is not a reporting tool, it can be used to document rules for interpreting testing results, creating reports, and understanding them.

Pros

  • Ease of use.
  • Real-time preview access.
  • Customizable to meet specific user needs.
  • Markdown language.
  • Convenient deployment on popular platforms such as GitHub.

Cons

  • Lacks a built-in database and content management features.
  • Complexity in managing documentation for multiple versions.

Pydoc

Pydoc – a tool within the Python ecosystem that automatically generates documentation from Python modules.

Key Features

  1. Saving Documentation in Various Formats: Documentation can be saved as a text page on the console, sent to a web browser, or saved in HTML format.
  2. Creating Documentation for Modules and Packages: This makes it invaluable for large projects.
  3. Command-Line Interface: Users can view documentation, save it as HTML, and perform other tasks using this interface.
  4. Local Access and Viewing: This feature is implemented by running an HTTP server that serves the generated documentation in a web browser.

Pros

  • As part of the Python toolkit, it requires no additional installations.
  • Intuitive interface for ease of use.
  • Automated approach to generating documentation.
  • Minimal setup required to get started.

Cons

  • Limited capabilities, such as lack of support for cross-references, custom themes, or advanced formatting options.
  • Supports only Python documentation.
  • Basic web interface, less advanced compared to other documentation tools.

Future of Python Libraries

The future of Python libraries is shaped by key trends in the software development market. Here are the main directions in which popular libraries will evolve:

  • Focus on Artificial Intelligence and Machine Learning: More machine learning libraries will become available to Python developers. Existing libraries in this area will continue to advance, improving their performance for effective machine learning tasks.
  • Performance and Efficiency Optimization: More libraries will adapt to use hardware acceleration. Frameworks and libraries supporting asynchronous programming will also become more prevalent.
  • Expansion of Libraries for Data Science: These libraries will feature seamless integration with big data platforms for efficient processing, as well as data visualization libraries.
  • Creation of Specialized Libraries: There will be an emphasis on modularity and reusability, leading to more small libraries for specific tasks.
  • Adherence to Security Standards: With growing cybersecurity threats, Python libraries will focus increasingly on secure coding practices, vulnerability detection, and other security measures.
  • Emphasis on Open Source: Open-source libraries will continue to grow with active support from the expanding community.
  • Interoperability Between Technologies: More libraries will aim for smooth interaction between different programming languages to bridge technology gaps.
  • Development of a Comprehensive Python Ecosystem: A key goal will be to ensure easy integration of Python libraries with IDEs, code editors, CI/CD tools, and testing automation for an improved experience for developers and testers.

In this overview of popular Python libraries, we have explored essential tools that facilitate the work of QA teams working on Python projects. In addition to libraries, we discussed popular documentation generators and Python code analyzers, which can also help optimize your workflows. We hope this article helps you choose a Python toolkit that best meets your needs.

👉 Would you like your Python library to be listed in the catalog on our website? Contact our manager for detailed consultation on partnership opportunities.

The post Popular Python Libraries <br> Make Python Testing More Efficient appeared first on testomat.io.

]]>
Basics of Python Unit Testing: How to Write, Organize and Execute Tests https://testomat.io/blog/a-guide-to-the-basics-of-python-testing-how-to-write-unit-tests-and-organize-execution-test-cases/ Sun, 28 Jul 2024 14:27:24 +0000 https://testomat.io/?p=15094 Unit tests play a critical role in software testing, as they allow teams to identify bugs in a digital product early in development – even before end users discover them. Unit testing is considered to be a rather expensive and time-consuming process for developers, but this statement is quite controversial when it comes to Python […]

The post Basics of Python Unit Testing: How to Write, Organize and Execute Tests appeared first on testomat.io.

]]>
Unit tests play a critical role in software testing, as they allow teams to identify bugs in a digital product early in development – even before end users discover them.

Unit testing is considered to be a rather expensive and time-consuming process for developers, but this statement is quite controversial when it comes to Python code testing. Thanks to the variety of flexible Python test frameworks, the challenge is more manageable. After all, this interpreted, object-oriented, high-level programming language offers Devs, SDETs and AQAs several tools that can be used to greatly simplify writing test cases and their execution.

Python Testing Basics: What Types of Testing Are Available?

Python3 is the latest version of the Python programming language, which features improved performance, more consistent syntax, and easier-to-understand code.

Python interface
Resource for Downloading Python packages

Python supports the following five types of testing of its programming code, which differ in the level of detail of the code being tested and the purpose of running tests:

  • Feature Testing. This is a type of QA process aimed at checking whether the functioning of a software product meets the requirements specified in the specification.
  • Python Unit Testing. This process tests a single test of a specific function or class.
  • Python Integration Testing. This stage implies complex testing of several components of a software product that are integrated with each other. This type of testing is usually performed after running unit tests.
  • Python Performance Testing. This type of testing allows you to evaluate the stability, speed, scalability, and responsiveness of a digital solution under a certain workload.
  • Python API Testing. This is a method for verifying the correctness of application interfaces, during which the server’s responses, status codes, the format of transmitted data, and the logic of exchange between the client and server are evaluated. For this, libraries such as requests, unittest, pytest, or httpx are most commonly used.

Popular Tools for Python Unit Testing

We made a short list of popular Python unit testing tools. These tools are widely used and discussed in Python communities and are quite effective in ensuring code reliability.

Tool Description Key Features
PyUnit logo Built-in Python testing framework based on Java’s JUnit; supports test discovery, fixtures, and assertions.
  • Test cases, suites, and fixtures for setup/teardown.
  • Object-oriented approach with TestCase class.
  • Automatic test discovery and detailed test reports.
PyTest logo A powerful, easy-to-use testing framework with rich plugin ecosystem and concise syntax.
  • Simple syntax with plain assert statements.
  • Powerful test discovery and rich plugin ecosystem (e.g., pytest-cov, pytest-xdist).
  • Supports unit, functional, and integration testing.
Doctest logo Tests embedded in documentation strings; good for simple, example-based testing.
  • Tests written as interactive Python sessions in docstrings.
  • Simple to use for small projects or documentation-driven testing.
yelp Testify logo A more modern alternative to unittest with additional features and cleaner syntax.
  • Decorator-based fixtures, reducing boilerplate.
  • Supports unit, integration, and system testing.
  • More Pythonic syntax than unittest.
Nose logo unittest extension with additional features and plugins.
  • Automatic test discovery and parameterized testing.
  • Plugin-based system for customization.
  • Supports multiprocessing for faster test execution.

You are free to choose automation Python unit testing based on your project type or testing needs (e.g. web app, project size, API, data science, CI\CD integration capabilities).

In this article, we will cover Python unit testing in detail with the unittest framework and focus on preparing for the QA process, writing tests and their execution.

Why Python unittest framework?

We chose unittest because it’s included in the Python standard library, doesn’t require extra setup, and integrates smoothly with your Python file. It also supports basic test organization using test case class, test file structures, and test module segmentation.

What Is Unittest Framework?

Unittest framework is a tool for unit testing that uses an object-oriented approach and provides users with basic features available in other popular frameworks – for example, JUnit. These include test automation, code sharing, combining tests into collections etc.

More interested in PyTest, then pay attention on these articles 👀

Getting to Know the Unittest Module

In this section, we will talk about how to write the unittest tests. We will touch on the concepts of the TestCase class, assert methods and look at options for running tests. We will walk you through writing your first test, structuring your project, and running test scripts via the command line or even using tools like the command CMD and VS Code.

Please note: as we noted on top ⬆ this framework is included in the Python standard library, so there is no additional setup required.

The Unittest testing tool supports some important concepts in realizing the key test functions.

🔑 Key Concepts of Unittest
Concept Meaning
Test fixture A list of actions required to prepare the test environment. This is the preparation that precedes the execution of one or a group of tests.
Test case A unit of testing that tests answers for specific datasets. The framework offers the TestCase class, i.e., a base class that allows the creation of test cases and test code.
Test suite A set of test cases for which co-execution is provided.
Test runner A tool responsible for test execution and providing a QA engineer with test results.
📁 Organizing Testing Code With TestCase Class

How to organize code with the TestCase class in Unit Testing Python? It is purely a test Structure in practice. To create your own test cases, define methods inside a test case class that start with the prefix test. Each piece of code inside this function is automatically picked up during the test run. Use assert statement variations to validate the return value against the expected value.

One way to create your own test cases in unittest is to write a subclass of the TestCase class. This subclass implements test methods, whose name starts with test, to execute the required testing code, look at example in code:

import unittest

class DefaultWidgetSizeTestCase(unittesst.TestCas):
    def test_default_widget_size(self):
        widget = Widget ('The widget')
        self.assert.Equal(widget.size(), (50,50))

Note that assert*() methods are used in this framework to perform validation. Here are the most frequently used ones:

assert method* What it checks?
assertEqual(a, b) is it true that a is equal to b – it verifies whether the actual output matches the expected result
assertNotEqual(a, b) is it true that a is NOT equal to b
assertTrue(x) is x a value True
assertFalse(x) is x a value False
assertIs(a, b) is it true that a is the same object as b
assertIn(a, b) is it true that a is present in b
assertNotIn(a, b) is it true that a is missing from b
assertAlmostEqual(a, b, places) is it true that a and b are approximately equal (to the specified number of decimal places)
assertIsNone(x) is it true that x is None
assertIsNotNone(x) is it true that x is NOT None
assertRaises() useful for checking error message handling, like in edge cases (e.g., division by zero).

How to Set up Unit Testing in Python?

Before you run Python Unit testing framework, there are some prerequisites to consider:

  1. Make sure you have an up-to-date version of Python installed on your system.
    python3 --version

    If you need it, you can download the Python source code and installers from the official website. At the time of writing, the latest version is Python 3.12.4.

  2. Choose a testing framework that best suits the needs of your project. There are many Python testing tools available on the market, the most popular are the Pytest, Doctest, PyUnit (unittest) frameworks – we’ll look at the last in detail below in this guide.
  3. Create a clear test project structure. This includes organizing all the tests, modules, and Python code files. This will optimize the testing process.
  4. Check the Python dependencies after installation. This can be done using the pip package management system. Use the pip check command to run the check.
    pip check

    If all dependencies are installed and compatible, the result will be as follows:

    // No broken requirements found.
  5. Create virtual environments. It ensures that the packages and their versions are specific to your project and do not interfere with the packages in other projects and vice versa.
    python -m venv /path/to/new/virtual/environment pip install virtualenv
    venv\Scripts\activate // Activate the virtual environment on Windows.
    source venv/bin/activate // Activate the virtual environment on macOS and Linux.

Select the supporting tools. For example, Visual Studio Code (VS Code), PyCharm editors:

JetBrains website for downloading PyCharm
Modern Python IDE PyCharm

A key advantage in the context of the topic at hand is their built-in support for Python unit testing. Third-party test Runners, Real-time test reporters, test coverage, CI\CD tools, etc.

VisualStudio code Python marketplace
Python PyTest, PyUnit, unittest Extensions

The command palette is a convenient tool that allows you to run editor functions, open files, search for symbols, view document structure, and more – all in one interactive window.

Breakpoints are particularly important for identifying the root cause of failures or unexpected behavior in automated tests. This IDE functionality is essential for Automation QA engineers in test debugging because it allows them to pause test execution at specific points to inspect the current state of the application or the test itself. Similarly, running a particular test from the code editor is possible.

Breakpoint in Visual Studio
Breakpoint IDE in Test Debugging

Once you have selected the tool that works best for you, you will be ready to perform unit testing – testing individual parts of the code to ensure that each one functions properly. Such tests help quickly identify issues in specific components of the program and promptly fix errors.

Write first Python unittest test

Suppose we have a class called Calculator, which contains methods for performing simple arithmetic operations: addition, subtraction, division, and multiplication:

Let’s demonstrate how testing in the unittest framework is performed across our knowledge:

  1. Set Up the Project Directory:
    my_calculator_project/
    │
    ├── calculator.py
    ├── test_calculator.py
    ├── requirements.txt
    └── venv/
  2. Create a Virtual Environment. Navigate to your project directory and create a virtual environment and do not forget to activate it:
    cd my_calculator_project
    python -m venv venv
    
  3. Create a calculator App.
    class Calculator:
        def add(self, a, b):
            return a + b
    
        def subtract(self, a, b):
            return a - b
    
        def multiply(self, a, b):
            return a * b
    
        def divide(self, a, b):
            if b == 0:
                raise ValueError("Cannot divide by zero")
            return a / b
    
  4. Write Unit Tests. Create a file named test_calculator.py for your unit tests.
    import unittest
    from calculator import Calculator
    
    class TestCalculator(unittest.TestCase):
    
        def setUp(self):
            self.calc = Calculator()
    
        def test_add(self):
            self.assertEqual(self.calc.add(1, 2), 3)
            self.assertEqual(self.calc.add(-1, 1), 0)
            self.assertEqual(self.calc.add(-1, -1), -2)
    
        def test_subtract(self):
            self.assertEqual(self.calc.subtract(2, 1), 1)
            self.assertEqual(self.calc.subtract(-1, 1), -2)
            self.assertEqual(self.calc.subtract(-1, -1), 0)
    
        def test_multiply(self):
            self.assertEqual(self.calc.multiply(2, 3), 6)
            self.assertEqual(self.calc.multiply(-1, 1), -1)
            self.assertEqual(self.calc.multiply(-1, -1), 1)
    
        def test_divide(self):
            self.assertEqual(self.calc.divide(6, 3), 2)
            self.assertEqual(self.calc.divide(-1, 1), -1)
            self.assertEqual(self.calc.divide(-1, -1), 1)
            
            with self.assertRaises(ValueError):
                self.calc.divide(1, 0)
    
    if __name__ == '__main__':
        unittest.main()
    
  5. Run the tests. With the virtual environment activated, run the tests using the following command:
    python -m unittest discover
    

    You should see output similar to this:

    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.001s
    
    OK
    

Benefits of the Unittest built-in simple Module

Using the Unittest module might streamline your Python automation testing workflow. By choosing the unittest framework for Python unit testing, your team will be able to discover certain positive aspects of this tool:

  • No need for additional installations. Unittest is part of Python’s standard library, allowing you to quickly get started testing without any prior configuration or installation.
  • Clear syntax. The Framework uses an object-oriented approach based on test classes, which is familiar to many developers.
  • Automated test discovery. This speeds up testing because the tool automatically discovers and runs all the tests in a catalog.
  • Built-in test runner. Testers have no need to turn to third-party tools. However, it is possible, if you want to do it. The framework integrates perfectly with other test runners.
  • Extensive functionality. This is achieved by supporting Test fixture, Test case, Test suite, and Test runner concepts.

All of the above benefits will become available to your QA team if you follow some tips for organizing Python unit testing, which we present below.

Interpret Results of Your Python Automated Testing

Interpreting the results of your Python automated tests is crucial to understanding software quality and spotting issues early. Each test outcome—pass, fail, or error—offers insight: passing tests confirm expected behavior, while failures point to broken functionality or mismatched assumptions.

Reviewing tracebacks, grouping results, and prioritization provide context and structure for your testing strategy. Clear test reports or dashboards further aid in communicating across stakeholders.

You can import your Python unit test from your test framework into the test management system testomat.io in this way:

Import Python Unit Test Framework
Import Python Unit Tests into Test Management

By executing them, you can review their outcomes, which are stored in a History Archive for tracking trends, finding bottlenecks. It is a great base for making decisions.

Tips for your Automation Python Testing Framework

Regardless of the tools you use to write unit tests and run them, it is recommended that you follow certain guidelines to get the best results.

Create clear test cases. It is better if they are short enough and easy to understand. To do this, use clear wording for test methods, which will make it obvious what code fragment is being tested. This allows you to understand the purpose of testing accurately.

Write isolated tests. Make sure that each of your tests does not depend on the results and state of other tests. This guarantees their stability due to their independence from external factors.

  • Carefully select the assertions you use. For this purpose, choose an assertion that matches the purpose of the test. It is also advisable to limit yourself to one assertion per test method.
  • When writing tests, consider boundary cases. These may be invalid input data, exceptional values, etc. This will help to detect unexpected behavior.
  • Include unit tests in the CI\CD pipeline. This will automatically run tests in case of changes in source code and ensure consistent execution.
  • Work on improving test coverage. Regularly study reports on test coverage of the code base to identify areas for improvement.
  • Don’t ignore test refactoring. Keep unit tests up-to-date and readable so that they retain their value as the code base grows.
  • Maintain quality test documentation. Add comments and descriptions for complex tests. This will help developers and other members of the QA team understand testing goals and optimize workflows.

Importance CI\CD in Python Automation Testing

CI\CD (Continuous Integration\Continuous Deployment) is important for unit testing because it ensures that tests are automatically and consistently run every time code changes are made. It provides confidence in deployment in a controlled environment. Development teams can safely push changes to production more frequently and receive feedback within minutes if their changes break the code.

The Right Python Unit Testing Framework for Long-Term Success

Python’s unittest is a robust test framework offering seamless test organization, clear syntax, and seamless integration with development tools. With lots of methods available for validation and easy-to-manage test data, it’s a great way to uphold high quality standards in any project. Whether you’re writing your first test case or maintaining complex systems, proper Python unit testing is essential for long-term success.

We hope this guide will help you optimize your Python testing process on your project by using a framework that allows you to create simple and reliable unit tests for your Python codebase.

Still have questions? Contact our expert and get detailed advice on all current trends in modern software testing.

The post Basics of Python Unit Testing: How to Write, Organize and Execute Tests appeared first on testomat.io.

]]>
Test API Python automation with a friendly Tkinter APP https://testomat.io/blog/test-api-python-automation-with-a-friendly-tkinter-app/ Tue, 30 Apr 2024 23:17:37 +0000 https://testomat.io/?p=13662 I’m very keen on programming, especially in Python and now I would like to show you its tool like Tkinter widely used for building Python Apps. I hope you like my tutorial very much.Let’s take a closer look at this combination below! Of course, you might think 🤔 Why not utilise all-in-one solutions? I chose […]

The post Test API Python automation with <br>a friendly Tkinter APP appeared first on testomat.io.

]]>
I’m very keen on programming, especially in Python and now I would like to show you its tool like Tkinter widely used for building Python Apps. I hope you like my tutorial very much.Let’s take a closer look at this combination below!

Of course, you might think 🤔 Why not utilise all-in-one solutions? I chose ✅ it for API testing using the Newman command instead of HTML reports because it can be very useful for short APIs or only to make sure that some microservices are working properly without requiring long reports and generating many files. Usually, I use it for microservice testing on my project App.

Before starting this guide, it would be a good idea if you had a look at some articles related to API tests with Postman and Newman, plus making your Python APPs with Tkinter:

Namely, they are:

And please make sure to pay attention to the article on this blog. Opting testomat.io testing tool also could serve as an alternative to get informative Python test reporting swiftly.

Pre Requirements:

  1. Install python3 and pip3 in your system
  2. Then, install the following Tkinter packages:
    sudo apt-get install python3-tk
  3. This project was created in an OS Ubuntu 20.04
  4. Install Newman in your system.
  5. Create a folder for saving the files of your Tkinter App.
  6. Export your Postman collection and Environment to the repository of your Tkinter App.
My API project looks like this screenshot

👉 Let’s start the creation of our Tkinter testing App. The full code you can find in my GitHub repository.

First of all, create a Python file called menu.py Next, import the tkinter lib and create a class called Application which will contain all the elements of the main menu. Then, call the screen by adding the if __name__ == "__main__": instructions:

Python class instruction and object on the program code

As a result, if you run your Python file, you should see the Thinker GUI window.

Python Thinker GUI

Create a new method within the class Application called screen with some information of the main menu and call this method when initializing the class:

Initializing Python class in our testing App

Create a new method within the class Application called screen_buttons including the information of the Python Tkinter style buttons in the main menu and call the method in the class initialization:

Python class to add style to button on Tkinter

Here are the button styles; You will see how they work in the following section of our Python Tkinter testing tutorial. The full code of our testing App is here

def screen_buttons(self): 
    self.bt_execute_api_tests = Button(self.main_screen,
 
            text="Execute API Tests", 
            border=2, 
            bg="#9ac7d2", 
            font=('verdana',10, 'bold'), 
            activebackground='#108ecb',
            activeforeground='white',
    command=self.openExecuteAPI
        ) 
    
    self.bt_execute_api_tests.place(
            relx=0.11, 
            rely=0.22,
            relwidth=0.2, 
            relheight-0.06
        ) 
    
    self.bt_quit = Button(self.main_screen,
 
            text="Quit Menu", 
            border=2, bg="#9ac7d2", 
            font=('verdana',10,'bold'), 
            activebackground='#108ecb', 
            activeforeground='white',
 
    command=self.main_screen.destroy
        ) 
    
    self.bt_quit.place(
            relx=0.11, 
            rely=0.52, 
            relwidth=0.2, 
            relheight=0.0
        )

Right after creating the method screen_buttons, create a new class called ScreenFunctions(), in which you put all the methods to redirect to the API tests page when we click on the button self.execute_api_tests.

Python implementation of redirect method

Now, create a new file under the APP folder called execute_api.py. Under this file (execute_api.py), import the lib Tk and then create a class also called as Application in which you should write the following code:

Call function

Come back to the file menu.py, and import the execute_api file by writing the following line:

Importing module of our App

Include the class ScreenFunctions within the Application class as Application(ScreenFunctions) in the file menu.py

Tkinter testing App launching

Execute the main menu again by opening the menu.py file. You should get the following screen:

Midway result of Tkinter testing App

If you click on the Execute API Tests, you should be brought to the screen initialized in the execute_api.py file. In the case you click on Quit Menu, the app will be closed.

Now, please open the file execute_api.py and let’s create the screen for executing our Newman tests through the methods attached to the buttons of the page by importing the Tkinter library in the execute_api.py file.

Our following step is to create some global variables identifying our folders and the Newman commands to execute each folder separately. In our example, our Postman collection has the following structure:

Structure of API project tree

Considering that, create the following variables.

🔴 Note: The Execution of all folder indicates that you want to execute the collection as a whole, not only one specific folder.

Variable commands_to_execute_tests indicates each Newman command to execute the tests for each collection folder.

Execution of Newman collection

As in the menu.py file, create the ScreenFunctions class and create a function for returning to the main menu. Include class ScreenFunction in the Application class (same as did for the menu.py file).

Create a method called screen under the class Application and call it in the initialization to set up the screen.

🔴 Obs: Import the menu file to make sure it will be called!

Implementation of the application menu

Also, create two variables called x and y that will be used to place some buttons with the name of each folder and execute all options as well.

Our variables

Under the ScreenFunctions class, create a function called create_button as the following code:

Method for the button of our testing App

Now, within the Application class, create a method called buttons which will be in charge of the creation of the buttons displayed on the screen.

Our Python method

Call this function in the initialization of the class Application

Initialization of our testing App

If you open your Python file execute_api.py, you should have the following result:

One more midway result of our Reporting Testing App on Tkinter CUI

Under the class Application, create a method called text_area which will send the terminal output generated from the Newman command execution. Call this method in the initialization:

Sending output

The last method within the class Application is called  scroll_bar_screen, it will be used to scroll up and down the text area for looking at the output information from Newman execution (also call it in the initialization of the class Application):

Scrolling

Import the following libraries in the file execute_api.py before starting the final steps. Also, create two more global variables called  stop_thread and call it.

Let’s do the final steps to show the results of the Newman command in our screen in the file execute_api.py. Under the class ScreenFunctions, create the following methods as shown below clock method.

For full code follow through the link

Now, enjoy your own Python Tkinter testing App 🙂 and execute your Newman tests in a friendly and easy way by clicking on the folder or executing everything, as you prefer. Hope you revolutionize your approach to API testing!

Real-time reporting Python Tkinter & Postman Newman API test collection

👉 Please let me know if you have any questions, reach out to me at my LinkedIn profile Pedro Carvalho. Once again I provide a Github repo of this project with full code. It will help you understand how the code works and how you can customize my experience for your test projects.

The post Test API Python automation with <br>a friendly Tkinter APP appeared first on testomat.io.

]]>
Python Playwright Tutorial for Web Automation Testing https://testomat.io/blog/python-playwright-tutorial-for-web-automation-testing/ Wed, 21 Feb 2024 23:43:35 +0000 https://testomat.io/?p=12640 In today’s digital era, web apps are widely utilized across different business verticals. Businesses use these apps as a way to interact with their customers, manage operations, and deliver services. That’s why it is crucial to check the app’s performance, functionality, speed, and reliability to deliver optimal user experience, minimize downtime, and maintain customer satisfaction. […]

The post Python Playwright Tutorial for Web Automation Testing appeared first on testomat.io.

]]>
In today’s digital era, web apps are widely utilized across different business verticals. Businesses use these apps as a way to interact with their customers, manage operations, and deliver services. That’s why it is crucial to check the app’s performance, functionality, speed, and reliability to deliver optimal user experience, minimize downtime, and maintain customer satisfaction. When it comes to verifying the quality and integrity of web applications, you should pay attention to web test automation. It entails automating user interactions with web elements, such as clicking buttons, submitting data, filling forms, and validating data, to deliver consistent and seamless behavior across different scenarios.

When repetitive testing tasks and activities are automated with web automation testing, it saves time, eliminates wasting time on manual work, and increases overall test coverage.

In the context of web automation testing, opting for end-to-end testing is a must!

Focusing on E2E testing and incorporating it into your testing process enables you to spot problems before they become major and take proactive measures. This ultimately will help you deliver a seamless user experience and drive great results.

⬇ 😃 Let’s jump in to find out more details in our Playwright Python tutorial and discover why Playwright and Python programming language are the perfect combination for e2e automation testing!

What is E2E testing?

End-to-end testing is utilized by test automation teams as a comprehensive software testing methodology. By simulating users’ behavior in different integration environments, end-to-end testing helps in detecting issues related to functionality, integration, performance, and usability across the entire application stack.

With E2E testing help, teams pay attention to every detail to make sure that all components function correctly and meet the desired requirements under various conditions and user scenarios. It also has a focus on building bug-free apps that fulfill business requirements.

What issues end-to-end testing might detect on a web application

  • Component Breakage due to JavaScript/CSS Errors. E2E testing can detect issues where components break or fail due to errors in JavaScript or CSS. This includes changes in element positions, renames, or modifications in attributes like URLs that could impact the application’s functionality.
  • Communication Issues with Services or Microservices. E2E testing provides insights into potential communication issues with backend services or microservices. By simulating user interactions, E2E tests can help teams detect where communication failures occur and handle them in a jiffy.
  • Version Update Compatibility. Maintaining the stability and performance of the application requires verifying compatibility across various versions of dependencies. That’s where E2E testing is instrumental in detecting compatibility issues that arise from version updates.
  • Packaging and Deployment Errors. E2E testing helps uncover errors related to how the software is packaged and deployed, such as issues with Kubernetes, Docker Compose, etc. configurations. By automating the testing process across various deployment environments, E2E testing saves time and provides consistent behavior across different setups.

Thus, incorporating E2E testing enables teams to proactively identify and address critical problems that lead to overall improvements in the quality, reliability, and user experience of web software products as well as drive faster deployment cycles and quicker time-to-market for new features and updates.

What exactly is Playwright technology?

You are to remember the Playwright test automation framework when it comes to end-to-end (E2E) testing. Playwright testing framework allows QA and development teams to automate interactions with web applications across different web browsers and platforms by simulating real users’ interactions and validating app functionality. Below you can find some key characteristics of Playwright:

  • Cross-Browser Testing. Playwright supports multiple modern browsers and assures that applications behave consistently across different browser environments and platforms.
  • Multi-Platform Support. Playwright’s compatibility with diverse OSs allows teams to conduct E2E testing across varied environments, facilitating comprehensive validation of applications on different platforms.
  • Automation Capabilities. Thanks to its powerful automation capabilities, Playwright enables testers to interact with web elements, navigate between pages, fill out forms, and validate UI elements.
  • Headless and Headful Testing. With both headless and headful testing modes, QA engineers have the option to choose: whether to run tests in headless mode for faster execution or to select a headful mode for debugging and visibility purposes.
  • Asynchronous Support. Playwright handles asynchronous operations seamlessly, enabling testers to wait for page elements to become interactable, handle network requests, and synchronize test execution with the application’s state effectively.

Benefits of your web testing automation with Playwright

Automating your web testing with Playwright brings a myriad of benefits to the table. It not only drastically reduces the manual effort of your development and testing teams and allows them to focus on higher-value tasks, but also drives thorough testing coverage, and improves the overall quality of your web applications. The additional advantages you can derive:

Why use Playwright Python for e2e?

Below we are going to take a closer look at five features that make Python Playwright more reliable and efficient than other tools for end-to-end testing.

#1: DevTools Protocol

The DevTools Protocol provides a seamless communication channel with the browser, orchestrating various browser functionalities and behaviors during test automation. From emulating user interactions and capturing network requests to manipulating browser settings – all these while carrying out consistent and reliable test executions. This execution is quick and less error-prone compared to alternatives. Even marginal time savings, measured in fractions of seconds, provide significant advantages in testing efficiency.

#2: Web-First Assertions and Auto-waiting approach

The Playwright’s features intelligently wait for elements to become actionable before executing. This means that tests interact with elements only when they are fully loaded and ready for interaction. In addition to that, software engineers can focus on behavior testing rather than writing code for each interaction. This approach streamlines the testing activities and allows developers and QA engineers to concentrate on writing meaningful test scenarios and assertions while spending less time managing waiting conditions.

#3: Parallel Test execution

Parallel test execution allows multiple test scripts to run across different browsers and environments. This capability optimizes resource utilization and drastically reduces time of test execution. Running tests in parallel significantly accelerates testing activities, facilitates quick feedback loops, and improves productivity. Development and QA teams not only scale their testing efforts effectively but are also aware of thorough validation of web applications while maintaining efficient testing workflows.

#4: Browser Context

With this feature, testing and development teams can test various scenarios (incognito sessions, multi-page interactions, etc.). It provides fast and accurate simulations of different browser environments, including incognito mode, which is valuable for testing persistent sessions between tabs or ensuring proper functionality under different browsing conditions. By leveraging browser context feature, QAs can effectively validate the behavior of web applications in diverse user scenarios and significantly improve the overall software product quality.

#5: Debugging and Recording

With built-in debugging tools such as breakpoints and step-by-step execution, developers can pinpoint and resolve issues quickly and improve the reliability and accuracy of their test scripts. Additionally, Playwright’s recording feature captures test interactions and generates complete logs or videos, facilitating collaboration and communication among team members. These debugging and recording capabilities enhance visibility into test executions and streamline the fixing process. This ultimately leads to web applications’ overall quality and reliability.

How to use Playwright Python for end-to-end testing:

With Playwright Python, the process of automating E2E tests becomes more streamlined and efficient. Below we will walk through the steps required to set up Python Playwright, write and execute E2E tests, and leverage its advanced features to create comprehensive test suites.

Step 1: Remember about Prerequisites

Before getting started, make sure that you have Python installed on your system. You can download and install Python from the official Python website. If not installed, you should find the latest Python version available for download and select the appropriate installer for your OS (Windows, macOS, and various Linux distributions). After the installation is complete, you can verify that the appropriate Python version is installed correctly by opening a command prompt or terminal window and typing the command:

python3 --version

This command will display the latest version of Python installed.

Download pip by running the following command if you have not done it before:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Install pip & check its version:

python3 get-pip.py
pip --version

Step 2: Set up the Project

You can start by creating a dedicated directory for your project where you will store your Python script or test scripts, configurations, and other project-related files. If you’re collaborating with other teams or want to track changes to your project, consider initializing a version control system like Git for your test automation project. In addition to that, you can create directories for configuration files, and any other resources.

Step 3: Install Playwright for Python

Once you have Python installed on your system, you can proceed installing Playwright for Python. Playwright can be installed using pip, the Python package manager that simplifies the installation process. In the command prompt, you need to type the following command:

pip install pytest-playwright

Check installed packages:

pip list

If only an update you need, then use the following:

pip install -U playwright
Checking downloaded packages

The next command that you need to run brings the browsers needed by Playwright:

playwright install

For example, installing Chrome only:

playwright install chrome browser

More detail in Playwright documentation

Step 4: Write your first Playwright test with Python

Once your project is set up, you can start creating your Python script using Playwright for Python. For instance we create it with the Playwright Codegen feature, an example of the test that checks the sign in testomat.io login page. So launch it with the command:

playwright codegen

Playwright Codegen will generate a test script code automatically by recording. This test script allows teams to check whether the website’s frontend and backend functionalities remain operational, providing a seamless experience for your real user and maintaining the App reliability.

Playwright Selectors and Locators

Selectors

Selectors are used to create Playwright locators that help testers find any element on the page. There are a few popular option of selectors applying in Playwright:

  • Text selectors –  use text content selectors to find elements based on their visible text content.
  • CCS selectors – target elements based on their attributes, classes, IDs, and other properties.
  • XPath selectors – navigate the XML structure of web pages and select elements based on their relationships within the document.
Locators

Locators –page.locator(selector[, options]) – are key components utilized to precisely detect and interact with elements via various methods, including click(), fill(), type(),etc.

First Python autotest with Playwright codegen

Step 5: Run the test with Pytest

Pytest will automatically execute these tests if the test filename begins with “test_” and ends in “.py”. If your tests encounter failures or errors, you can utilize Playwright’s debugging features to troubleshoot and identify the problem and refine your test scripts as needed to improve their reliability and effectiveness

pytest

Step 6: Analyze and Report

Monitor the test execution output to see the results and any potential errors or failures. You can see detailed logs and information to help you understand the test outcomes in Playwright directly.

Additionally, you can integrate a test management system to display more detailed information about test cases, test runs, and test results with automated reporting and synchronization features.

Comprehensive Analytics Dashboard with Test Management
Comprehensive Analytics Dashboard

By following these steps, you can effectively use Playwright with Python programming language for end-to-end testing of your web applications. Playwright with Python is an ideal choice for automating your testing workflows and driving the quality of your web applications.

Learn more with posts:

  • Playwright vs Cypress: Which Platform to Choose for Web App Testing
  • 🎭 Test Automation with Playwright: Definition and Benefits of this Testing Framework
  • How To Capture Screenshots & Videos — Playwright JS Tutorial
  • Playwright Testing Tutorial on How to Organize an Advanced (Scaled) (e2e/unit) Testing Framework

Why development & testing teams choose Python Playwright for e2e?

Here are some key reasons why developers and testers opt for Python Playwright for e2e testing:

  • They can effortlessly run tests across major browsers like Chrome, Firefox, Microsoft Edge, and Safari without the need for separate tools for each browser.
  • Intuitive and Python-friendly API allows developers and testers to focus on the functionality of their tests without struggling with the complex syntax of code.
  • Developers and QAs can simulate real-world user experiences on various mobile devices and screen sizes to deliver apps that function flawlessly across different platforms.
  • They can leverage plugins and extensions for popular IDEs (e.g., Visual Studio Code) to conduct a smoother development process.
  • With great visual debugging functionality, teams can pinpoint issues quickly and accurately.

Best Practices for Successful Playwright Python e2e Testing

The following practices will help you write effective and efficient e2e tests using Playwright Python to deliver stable and high-quality web applications. Let’s dive into the details below:

  • Understand application requirements and user flows and define what you’re going to test before writing any code.
  • When creating test cases, it is essential to use descriptive test names that indicate what each test is testing for easier understanding and organization.
  • Each test should focus on a single specific functionality that makes your tests easier to understand and update.
  • Try to choose appropriate assertions that verify the expected behavior of the application.
  • Take advantage of Playwright’s parallel tests execution feature to speed up test execution, especially for larger test suites.
  • Set up continuous integration and continuous delivery (CI/CD) pipelines to run tests automatically after every code change.
  • Choose appropriate test runners like Pytest to manage your test cases and provide additional functionalities.
  • Implement reporting mechanisms to track test execution, analyze results, and identify potential issues.

Bottom line:

Ready to revolutionize your test automation process with Python Playwright?

When choosing Playwright Python for test automation, you can get a plethora of features that streamline the testing process.

Your development and testing teams can conduct thorough validation of the web applications’ functionality, performance, and reliability and make sure that they meet the desired requirements. Furthermore, the ability to integrate with TMS improves collaboration and delivers real-time insights into test results. This significantly facilitates continuous improvement and delivery of high-quality software products.

If your organization strives for agility and efficiency in the software development lifecycle, the Python Playwright combination will help your teams achieve comprehensive test coverage and deliver products and features that get users to value quickly.

👉 Drop us a line if you need more information about web automation testing with Playwright Python!

The post Python Playwright Tutorial for Web Automation Testing appeared first on testomat.io.

]]>
How to stop worrying and learn to love Playwright🎭 https://testomat.io/blog/how-to-learn-playwright/ Mon, 07 Nov 2022 09:56:04 +0000 https://testomat.io/?p=4496 If you are wondering – why Python? Playwright supports JS natively! If you are not sure – try a programming language you like. I develop using Python because I love it ❤️. I know about its pros and cons (every programming language has its pros and cons), but I enjoy using it in my development […]

The post How to stop worrying<br> and learn to love Playwright🎭 appeared first on testomat.io.

]]>
If you are wondering – why Python? Playwright supports JS natively! If you are not sure – try a programming language you like. I develop using Python because I love it ❤. I know about its pros and cons (every programming language has its pros and cons), but I enjoy using it in my development and automation tasks. I believe it is very important to enjoy your job, whatever you are doing 🙂

I’m 100% sure Playwright – the best thing that could happen with web test automation!

It is fast, reliable and user-friendly. Furthermore – new releases are published regularly, issues are fixed, community – growing!

Let’s leave all concerns about the pros and cons of test automation itself for another time and check what awesome you can do with Playwright, if you have never heard about it.

Top 5 awesome Playwright features

1#: Playwright can work with web browser network requests

This feature sold me the framework 2 years ago. Just imagine – your test can open a website, do some steps which cause ajax request to the backend. And you have instruments to catch, analyze, replace request and even replace response! Previously negative tests were really hard to automate in E2E testing – you can’t just make a backend to respond HTTP 400 or HTTP 500 whenever you want – you need some mock servers, complicated infrastructure and tests.

Now I can cause a BE error with a few lines of code! But wait, it is more – if you want to send some additional HTTP requests from your browser, using the same web session with all headers, tokens, cookies, just do it with a single command! Check out documentation: API testing and request routing.

# response fulfill
page.route("**/*", lambda route: route.fulfill(
   status=404,
   content_type="text/plain",
   body="not found!"))
page.click(".donetworkAction")

# send request
response = page.request.get("/response")
expect(response).to_be_ok()

#2: Selectors – one love!

When I used to work with Selenium, I always struggled with some tricky elements, hard to locate. No, I’m just having fun!

  • Except of generic selectors like CSS, XPath, id or label, Playwright devs added a lot of CSS pseudo-classes to improve search
  • There is a possibility to chain selectors. If you ever dreamed of using XPath and CSS in a single selector – just do it!
  • Moreover, you can select not the last element in the selector chain, but the element in the middle! Useful, if you need to locate some div by a tricky element inside of it. In XPath I’ve used scary constructions like parent and child
  • If searched element changes in time, you can put several selectors and make Playwright find it through one of them
  • Your front-end developers like to use iframes? Don’t worry – Playwright handles them natively
# pseudo class :has finds element which has elements
page.locator('tbody tr:has(.delete_1) .passBtn').click()


# selectors chain. * selects element in the middle
page.locator('xpath=//table >> *css=tr >> text="login"').click()


# Clicks a <button> that has either a "Log in" or "Sign in" text.
page.locator('button:has-text("Log in"), button:has-text("Sign in")').click()


# Locate element inside frame
# Get frame using any other selector
username = page.frame_locator('.frame-class').get_by_label('User Name')
username.fill('John')

3#: Work with web browser context

Earlier I always had issues, if my test opens a link in a new tab. I’ve tried to avoid such cases. If I had a test with 2 users doing some scenarios, I’d often skip them, because test were long and unstable. But now Playwright allows to create separate browser contexts to keep user sessions isolated. If I need a few tabs – just create them and cat manage at any moment!

context1 = chromium.new_context()
page1 = context1.new_page()
page1.fill('#username', 'alice')
page1.fill('#password', 'alice')
page1.click('text="Login"')
context2 = chromium.new_context()
page2 = context1.new_page()
page2.fill('#username', 'bob')
page2.fill('#password', 'bob')
page2.click('text="Login"')
page1.click(".button")
page2.click(".button")

4#: Playwright is a resilient and auto-waiting element tool

Forget stuff like checking ElementNotFoundException after each test record if, the element appears in DOM 1 second later than expected. You can just type selector and enjoy the results

5#: Playwright is headless out of the box.

I was surprised in the very beginning – how to check and debug why my test failed? But then I;ve realized – tests work as you expect – no need to debug them too often! 🙂

Out of this top I have to mention the async mode of using Playwright. You will not need it in 99% of cases, but I had 1% when I needed to do weird performance test with real browser instances. And I did it!

from playwright.browser import Browser

import asyncio
from playwright import async_playwright
import random

BASE_URL = 'https://qamania.org/'
PAGES = (('Home', 'QA Mania'), ('Blog', 'Blog'), ('Useful links', 'Useful links'))

# here you can pass login/password to test function
async def open_test(browser: Browser, data: tuple, id: int):
    print(f'start #{id} thread')
    context = await browser.newContext()
    page = await context.newPage()
    await page.goto(BASE_URL)
    await page.click(f'text={data[0]}')
    title = await page.title()
    result = 'ok' if data[1] in title else 'not ok'
    print(f'Thread #{id}: result for page {data[0]} --> {result}')
    await page.close()
    await context.close()

async def main():
    print('start main')
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        tasks = list()
        for thread in range(10):
            tasks.append(open_test(browser, random.choice(PAGES), thread))
        await asyncio.gather(*tasks)
        await browser.close()

asyncio.run(main())

That’s my Playwright overview!

I am glad to share my top with you 🤗 Take the Playwright testing framework for a spin.

Last year I published a course “Test automation with Playwright and Python” on the Udemy testing course marketplace in Ukrainian language. Then I decided to publish it for free with English subtitles on Youtube due to my followers grow proficiently without any obstacles. It is still awesome. If you are not worrying about Ukrainian, you can check videos or code on Github Have fun and let’s get testing!

Note, the JUnit XML Format Support testomat.io test management system allows you to consolidate all your Python Playwright automated tests and manual tests together in one play for analysis and better decision-making. And that is yet another reason for getting started with Playwright.

Let’s Talk…

  • What feature are you interested in the most?
  • Or if you are already using Playwright, what top features do you have?

Please feel free to spread your opinion in a comment, I would be glad to discuss the above with you.

The post How to stop worrying<br> and learn to love Playwright🎭 appeared first on testomat.io.

]]>