Search code examples
python-3.xseleniumdata-driven-testsdata-driven

Selenium Data Driven Testing in Python


How to do data-driven testing in selenium with python, and in what kind of case do we need to data-driven test?


Solution

  • Use pytest library something as below code demonstrates.

    import pytest
    
    @pytest.mark.parametrize("num1, num2, output", [(10, 2, 5), (20, 10, 2)])
    def testDivisionWithPositiveValues(CreateCalculatorClassObj, num1, num2, output):
        result = CreateCalculatorClassObj.Division(num1, num2)
        assert result == output
    

    In the above code @pytest.mark.parametrize is from pytest library and it is used where you want to pass the data to the method. The mothod annotated with this annotation will automatically pick the data from it.