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

Functional vs Regression Testing

Functional Testing vs Regression Testing: Main Differences

Testing has increasingly become a requirement in the software development lifecycle in recent years. The realization that development is no longer a focal point for stakeholders but saving costs by catching defects early in their application makes it necessary to learn different testing types.

Read More »