Bird
Raised Fist0
Selenium Pythontesting~10 mins

Test functions and classes in Selenium Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a test function using pytest.

Selenium Python
def [1]():
    assert 2 + 2 == 4
Drag options to blanks, or click blank then click option'
Aaddition_test
Btest_addition
Ccheck_add
Dverify_sum
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the function without 'test_' prefix so pytest ignores it.
Using spaces or special characters in the function name.
2fill in blank
medium

Complete the code to import the Selenium WebDriver for Chrome.

Selenium Python
from selenium.webdriver import [1]
Drag options to blanks, or click blank then click option'
AChrome
BFirefox
CEdge
DSafari
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Firefox or other browsers when Chrome is needed.
Misspelling the browser name.
3fill in blank
hard

Fix the error in the test class definition for pytest.

Selenium Python
class [1]:
    def test_example(self):
        assert True
Drag options to blanks, or click blank then click option'
ATestExample
BExampleTest
CexampleTest
Dtestexample
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or names not starting with 'Test' for test classes.
Forgetting to capitalize the class name properly.
4fill in blank
hard

Fill both blanks to create a Selenium test that opens a page and checks the title.

Selenium Python
from selenium import webdriver

class TestPageTitle:
    def test_title(self):
        driver = webdriver.[1]()
        driver.get('https://example.com')
        assert driver.title [2] 'Example Domain'
        driver.quit()
Drag options to blanks, or click blank then click option'
AChrome
B==
C!=
DFirefox
Attempts:
3 left
💡 Hint
Common Mistakes
Using Firefox WebDriver when Chrome is intended.
Using '!=' instead of '==' in the assertion.
5fill in blank
hard

Fill all three blanks to create a pytest test function that finds an element by ID and asserts its text.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import [1]

def test_element_text():
    driver = webdriver.Chrome()
    driver.get('https://example.com')
    element = driver.find_element([2], 'example-id')
    assert element.text [3] 'Example Domain'
    driver.quit()
Drag options to blanks, or click blank then click option'
ABy
BBy.ID
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Not importing 'By' and trying to use it directly.
Using wrong locator like 'By.NAME' instead of 'By.ID'.
Using '!=' instead of '==' in assertion.

Practice

(1/5)
1. What is the main purpose of using test functions and classes in Selenium Python testing?
easy
A. To organize tests clearly and run them easily
B. To make the code run faster
C. To avoid writing any assertions
D. To skip browser setup

Solution

  1. Step 1: Understand test organization

    Test functions and classes help group related tests logically.
  2. Step 2: Recognize ease of running tests

    Using classes and functions allows running tests easily with frameworks like unittest.
  3. Final Answer:

    To organize tests clearly and run them easily -> Option A
  4. Quick Check:

    Test organization = To organize tests clearly and run them easily [OK]
Hint: Tests group checks for clarity and easy running [OK]
Common Mistakes:
  • Thinking tests speed up code execution
  • Believing assertions are optional
  • Ignoring setup and teardown roles
2. Which of the following is the correct way to define a test method inside a unittest.TestCase class in Selenium Python?
easy
A. def test_example(self):
B. def example_test():
C. def TestExample(self):
D. def _test_example(self):

Solution

  1. Step 1: Recall unittest naming conventions

    Test methods must start with 'test' and accept 'self' as first parameter.
  2. Step 2: Check method definitions

    Only 'def test_example(self):' follows correct naming and signature.
  3. Final Answer:

    def test_example(self): -> Option A
  4. Quick Check:

    Method name starts with 'test' and has 'self' [OK]
Hint: Test methods start with 'test' and include self [OK]
Common Mistakes:
  • Omitting 'self' parameter
  • Not starting method name with 'test'
  • Using uppercase 'Test' prefix
3. What will be the output when running this test code snippet?
import unittest

class MyTest(unittest.TestCase):
    def test_pass(self):
        self.assertEqual(2 + 2, 4)

    def test_fail(self):
        self.assertTrue(False)

if __name__ == '__main__':
    unittest.main(exit=False)
medium
A. Both tests pass
B. One test passes, one test fails
C. Both tests fail
D. Syntax error occurs

Solution

  1. Step 1: Analyze test_pass method

    assertEqual(2 + 2, 4) is True, so test_pass passes.
  2. Step 2: Analyze test_fail method

    assertTrue(False) fails, so test_fail fails.
  3. Final Answer:

    One test passes, one test fails -> Option B
  4. Quick Check:

    One True, one False assertion = One test passes, one test fails [OK]
Hint: Check each assertion's truth to predict pass/fail [OK]
Common Mistakes:
  • Assuming all tests pass
  • Ignoring assertTrue(False) fails
  • Expecting syntax errors
4. Identify the error in this Selenium Python unittest code:
import unittest
from selenium import webdriver

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_login(self):
        self.driver.get('http://example.com')
        self.assertIn('Example', self.driver.title)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
medium
A. webdriver.Chrome() requires path argument
B. Missing call to super() in setUp and tearDown
C. No error; code is correct
D. assertIn should be assertEquals

Solution

  1. Step 1: Review setup and teardown methods

    setUp creates driver; tearDown quits it properly.
  2. Step 2: Check assertion and webdriver usage

    assertIn checks substring in title correctly; webdriver.Chrome() works if driver is in PATH.
  3. Final Answer:

    No error; code is correct -> Option C
  4. Quick Check:

    Standard unittest setup and teardown used [OK]
Hint: Check if setup/teardown and assertions follow unittest rules [OK]
Common Mistakes:
  • Assuming webdriver.Chrome() always needs path
  • Confusing assertIn with assertEquals
  • Forgetting to quit driver in tearDown
5. You want to run multiple Selenium tests that share the same browser setup and cleanup. Which unittest structure best achieves this?
hard
A. Call unittest.main() inside each test method
B. Write separate test functions without any setup or teardown
C. Create a new webdriver instance inside each test method
D. Use a TestCase class with setUpClass and tearDownClass class methods

Solution

  1. Step 1: Understand shared setup/cleanup needs

    setUpClass and tearDownClass run once for all tests in a class, ideal for shared browser setup.
  2. Step 2: Evaluate other options

    Separate functions lack shared setup; new webdriver per test is inefficient; unittest.main() runs all tests, not per method.
  3. Final Answer:

    Use a TestCase class with setUpClass and tearDownClass class methods -> Option D
  4. Quick Check:

    Shared setup/cleanup = setUpClass/tearDownClass [OK]
Hint: Use setUpClass/tearDownClass for shared browser setup [OK]
Common Mistakes:
  • Creating driver in each test wastes resources
  • Calling unittest.main() multiple times causes errors
  • Skipping setup causes flaky tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard