Question: I'm practicing writing test cases for web automation and I have written functions to test login, find my username in the user home page and test logout functionality of GitHub. However, I've learned through both experience and reading that setUp() is initiated before each test method, and my problem is that before every test method it opens a new browser. I want all my test methods to continue testing on the same browser in the same session. Here is my code to show you what I've tried: import unittest from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support.expected_conditions import element_to_be_clickable class GitHubLoginTest(unittest.TestCase): initialized = 0 completed = 0 def setUp(self): if self.initialized < 1: self.initialized = 1 chrome_options = Options() chrome_options.add_experimental_option("detach", True) self.driver = webdriver.Chrome(options=chrome_options) else: pass def test_login(self): driver = self.driver driver.get("https://github.com") driver.find_element(By.LINK_TEXT, "Sign in").click() username_box = WebDriverWait(driver, 10).until(element_to_be_clickable((By.ID, "login_field"))) username_box.send_keys("<username>") password_box = driver.find_element(By.NAME, "password") password_box.send_keys("<password>") password_box.submit() self.completed += 1 print(self.completed) print(self.initialized) def test_username_presence(self): print(self.completed) print(self.initialized) self.assertIn("SubjectofthePotentate", self.driver.page_source) self.driver.find_element(By.CLASS_NAME, "AppHeader-user").click() profile_label = self.driver.find_element(By.CLASS_NAME, "lh-condensed") user_label = profile_label.get_attribute("innerHTML") print(user_label) self.assertIn("SubjectofthePotentate", user_label) self.completed += 1 print(self.completed) def test_logout(self): self.driver.find_element(By.CLASS_NAME, "DialogOverflowWrapper") self.driver.find_element(By.ID, ":r11:").click() sign_out = WebDriverWait(self.driver, 10).until(element_to_be_clickable((By.NAME, "commit"))) sign_out.click() self.completed += 1 print(self.completed) def tearDown(self): if self.completed == 3: self.driver.close() else: pass if __name__ == "__main__": unittest.main()  I tried creating attributes which I called initialization and completed to prevent setUp() from loading another browser and also to prevent tearDown() from closing the browser before all the tests are finished, but a new browser is opened three times, one for each test function. I noticed that when print(self.completed) and print(self.initialized) are executed in the test_login(self) method that they both equal 1, but when print(self.completed) and print(self.initialized) are executed again in the test method test_username_presence(self), self.completed is equal to 0 and self.initialized is equal to 1, so I think that means that the setUp(self) method is being executed before each test method and the attributes I defined at the class level are being reset for some reason. I've tried initializing these attributes using setUpClass(cls) and I also tried this: def __init__(self, other): super().__init__(other) self.completed = 0 self.initialized = 0  But I got the same results, multiple browsers and the last two are empty webpages with no URL. Anyone know how I can continue the tests on one browser in one session?

Answer: In python unittest you have 3 levels of setup / teardown.  Method level: setUp / tearDown: called before / after each test method. Class level: setUpClass / tearDownClass: called before / after tests in an individual class. Module level: setUpModule / tearDownModule: called before / after tests in an individual module.  So in your case you want to define class level setup / teardown. @classmethod def setUpClass(cls): pass @classmethod def tearDownClass(cls): pass  See also, https://stackoverflow.com/a/23670844/4413446.