01. Intro
By using the data-driven testing approach, we separate the test data from the test logic by replacing the hardcoded test data with variables using the data from external sources such as CSV or a spreadsheet file.
02. Installing ddt(Data-driven testing)
pip install ddt
03. Decorator for the ddt
To create a data-driven test, you need to use the @ddt decorator for the test class.
@data decorator is used on the data-driven test methods. This takes as many arguments as we have values that we want to feed to the test. These could be single values or lists, tuples, and dictionaries.
For lists, we need to use the @umpack decorator, which unpacks tuples or lists into multiple arguments.
04. import and declare @ddt
import unittest
from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
@ddt
class SearchDDT(unittest.TestCase):
def setUp(self):
05. Test code
# specify test data using @data decorator
@data(('phones', 1), ('music', 5))
@unpack
def test_search(self, search_value, expected_count):
# get the search textbox
search_field = self.driver.find_element(By.NAME, 'q')
search_field.clear()
# enter search keyword and submit
# use search_value argument to pass data
search_field.send_keys(search_value)
search_field.submit()
# get all the anchor elements which have product names
products = self.driver.find_elements(By.XPATH, "//h2[@class='product-name']/a")
# check count of products in result
self.assertEqual(expected_count, len(products))
Reference: Learning Selenium Testing Tools with Python by Unmesh Gundecha
'Testing Automation > 01. Selenium' 카테고리의 다른 글
09. Selenium | Switch_to (0) | 2022.03.19 |
---|---|
08. Selenium | External data sources for DDT (0) | 2022.03.09 |
06. Selenium | Wait ( Implicit / Explicit) (0) | 2022.03.05 |
05. Selenium | Alert (0) | 2022.03.04 |
04. Selenium | Browser navigation (0) | 2022.03.04 |