Optimize Selenium Python tests with Setup & Teardown methods

Image for post
Image for post
https://youtu.be/uZ-sgPTM0eM

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 –

Image for post
Image for post

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

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 –

Image for post
Image for post

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

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

📧 Subscribe to my mailing list to get access to more content like this

👍 Follow automationbro on Twitter for the latest updates

This post was written with the help of a high amount of caffeine. And, if this post helped you out and you would like to support my work, you can do that by clicking on the button below and continue supporting my caffeine love :)

Image for post
Image for post

You can also support me by liking and sharing this content.

Thanks for reading!

SDET Architect | YouTuber | Tech Blogger | Love to explore new tools and technologies. Check out my YouTube channel — https://youtube.com/automationbro

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store