SDET Unicorns

Optimize tests with Setup & Teardown methods

Setup & TearDown methods

Table of Content

In this post, we will learn how we can use SeleniumBase Setup & Teardown methods to optimize our tests even further. If you are not familiar with setup & teardown concepts, essentially, it allows you to run certain steps before and after your actual test.

So, when you run the Setup method, it will trigger the steps BEFORE your test will run and the TearDown will run the steps AFTER your test has finished.

Setup Method

The setup method will run the steps before each of your tests. Let’s take a look at the sample code below –

    def setUp(self):
        # Call the parent BaseCase class setup method
        super().setUp()
        print("RUNNING BEFORE EACH TEST")

        # LOGIN
        self.open("https://practice.automationbro.com/my-account")
        self.add_text("#username", "testuser")
        self.add_text("#password", "testuser123")
        self.click("button[name=login]")
        self.assert_text("Log out", ".woocommerce-MyAccount-content")

        # open home page
        self.open("https://practice.automationbro.com")

Typically, you would use your setup method for any repetitive code between your tests such as opening the page, test data creation or Login.

TearDown method

The tearDown method will run the steps after each of your tests has finished running. Let’s take a look at the sample code below –

    def tearDown(self):
        print("RUNNING AFTER EACH TEST")

        # Logout
        self.open("https://practice.automationbro.com/my-account")
        self.click(".woocommerce-MyAccount-content a[href*=logout]")
        self.assert_element_visible("button[name=login]")
        
        # Call the parent BaseCase class tearDown method
        super().tearDown()

Typically, you would use tearDown method for actions such as taking screenshot after test has finished running, logging some data or for Logout.


Check out the video below to learn more about the Setup & TearDown methods –


To learn more about Selenium Python, check out the full tutorial series here for free!

I hope this post helped you out, let me know if you have any questions in the comments below!

Happy testing! 

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »