Base Test

Introduction

In a Selenium automation framework, almost every test begins with the same setup steps, such as launching the browser, creating a WebDriver instance, and navigating to the required webpage. Similarly, every test ends by closing the browser and cleaning up resources.

If these setup and teardown steps are written inside every test file, the framework quickly becomes repetitive and difficult to maintain. A better approach is to place this common functionality into a Base Test class.

A Base Test acts as the foundation for all test classes. It manages browser initialization and cleanup automatically, allowing individual test cases to focus only on the business logic being tested.

In this tutorial, you’ll learn what a Base Test is, why it is used in Selenium frameworks, and how it simplifies test automation.


What is a Base Test?

A Base Test is a parent test class that contains common setup and teardown logic shared by multiple test classes.

Instead of creating and closing the browser inside every test, the Base Test handles these operations automatically using PyTest Fixtures.

Child test classes simply inherit from the Base Test and immediately gain access to the configured WebDriver instance.


Why Use a Base Test?

Using a Base Test provides several benefits:

  • Eliminates duplicate setup and teardown code.

  • Keeps test cases clean and focused.

  • Centralizes browser initialization.

  • Simplifies framework maintenance.

  • Ensures consistent test execution.

  • Makes the framework easier to scale.


How Base Test Fits into a Framework

                    BaseTest
                        │
        ┌───────────────┼───────────────┐
        │               │               │
   LoginTests      ProductTests     CheckoutTests
        │               │               │
        └────── Uses Shared WebDriver ──┘

The Base Test creates and closes the browser, while child test classes contain only the test scenarios.


Example

import pytest
from selenium import webdriver


# Topic: 45. Framework Components - Base Test
# Practice site: https://www.testmuai.com/selenium-playground/
# Run: pytest -s 45_examples/test_02_base_test.py
#
# BaseTest sets up and tears down the browser so feature tests stay focused on
# business steps.


class BaseTest:
    @pytest.fixture(autouse=True)
    def setup_browser(self):
        self.driver = webdriver.Chrome()
        yield
        self.driver.quit()


class TestPlaygroundHome(BaseTest):
    def test_base_test(self):
        self.driver.get("https://www.testmuai.com/selenium-playground/")
        assert "selenium-playground" in self.driver.current_url

Understanding the Code

Import Required Libraries

import pytest
from selenium import webdriver
  • pytest is used to create and manage fixtures.

  • webdriver is used to launch and control the Chrome browser.


Create the Base Test Class

class BaseTest:

A class named BaseTest is created.

This class stores the common browser setup and teardown logic that will be shared by multiple test classes.


Create a PyTest Fixture

@pytest.fixture(autouse=True)

The @pytest.fixture decorator creates a fixture.

Setting autouse=True means the fixture runs automatically before and after every test method in classes that inherit from BaseTest.

There is no need to call the fixture manually.


Create the Browser

self.driver = webdriver.Chrome()

A new Chrome WebDriver instance is created and stored in self.driver.

Since it is stored as an instance variable, every test method inside the child class can access it directly.


Pause Test Execution

yield

The yield statement separates the fixture into two parts:

  • Code before yield is the setup.

  • Code after yield is the teardown.

PyTest automatically executes the teardown after the test finishes.


Close the Browser

self.driver.quit()

After the test completes, driver.quit() closes the browser and ends the WebDriver session.

This ensures that browser resources are cleaned up properly after every test.


Create a Child Test Class

class TestPlaygroundHome(BaseTest):

The TestPlaygroundHome class inherits from BaseTest.

Because of inheritance, it automatically receives the configured self.driver without creating a browser itself.


Open the Practice Website

self.driver.get(
    "https://www.testmuai.com/selenium-playground/"
)

The test uses the shared WebDriver from the Base Test to open the Selenium Playground website.


Verify the URL

assert "selenium-playground" in self.driver.current_url

The assertion verifies that the browser successfully navigated to the expected webpage.


Practical Example

Suppose an e-commerce application contains separate test classes for:

  • Login

  • Products

  • Shopping Cart

  • Checkout

  • User Profile

Instead of creating a browser inside every test class, each class inherits from the Base Test, allowing browser setup and cleanup to happen automatically.


Automation Testing Example

Consider an enterprise banking application containing hundreds of automated test cases.

Every test requires a browser session before execution and proper cleanup afterward.

A Base Test centralizes this functionality, ensuring every test follows the same setup and teardown process while reducing duplicate code.


Real-World Example

Base Test classes are commonly used in:

  • Banking applications

  • E-commerce platforms

  • Healthcare systems

  • Insurance portals

  • CRM applications

  • ERP software

  • SaaS products

  • Enterprise automation frameworks

Professional Selenium frameworks almost always include a Base Test to standardize browser management.


Advantages of Using a Base Test

  • Eliminates repetitive setup code.

  • Automatically handles browser initialization.

  • Automatically performs cleanup after tests.

  • Keeps test classes simple and readable.

  • Promotes code reuse.

  • Improves framework maintainability.

  • Supports scalable automation projects.


Common Mistakes Beginners Make

Creating the Browser Inside Every Test

Avoid launching webdriver.Chrome() inside every test method.

Move common setup into the Base Test.


Forgetting to Close the Browser

Leaving browser instances open can consume system resources.

Always close the browser during teardown using driver.quit().


Not Using Fixtures

Without PyTest Fixtures, setup and teardown code must be repeated in every test class.

Fixtures make the framework cleaner and more maintainable.


Not Using Inheritance

If test classes do not inherit from BaseTest, they cannot reuse the shared browser setup.


Best Practices

  • Keep browser setup inside the Base Test.

  • Use @pytest.fixture(autouse=True) for automatic execution.

  • Store the WebDriver in self.driver.

  • Use yield to separate setup and teardown.

  • Let all test classes inherit from the Base Test.

  • Keep only reusable setup logic inside the Base Test.


Conclusion

A Base Test is an essential component of a Selenium automation framework. It centralizes browser setup and teardown, allowing individual test classes to focus only on test scenarios. By combining PyTest Fixtures with inheritance, the Base Test reduces duplicate code, improves readability, and creates a more maintainable and scalable automation framework.


Frequently Asked Questions (FAQs)

What is a Base Test?

A Base Test is a parent class that manages common test setup and teardown operations for multiple test classes.


Why is a Base Test used?

It eliminates duplicate browser initialization and cleanup code, making the framework cleaner and easier to maintain.


What is the purpose of @pytest.fixture(autouse=True)?

It automatically executes the setup and teardown fixture before and after every test without requiring explicit calls.


Why is yield used in the fixture?

The code before yield performs setup, while the code after yield performs teardown once the test finishes.


Should every test class inherit from the Base Test?

Yes. In most Selenium frameworks, all test classes inherit from the Base Test so they can reuse the common browser setup and teardown logic.


Key Takeaways

  • A Base Test centralizes browser setup and teardown.

  • @pytest.fixture(autouse=True) runs the fixture automatically for every test.

  • yield separates setup from teardown.

  • Child test classes inherit the shared WebDriver using BaseTest.

  • A Base Test reduces duplicate code and improves framework maintainability.

  • Most professional Selenium automation frameworks use a Base Test as a core framework component.