Pytest Coding
Pytest Coding
Import pytest
def pytest_adoption(parser):
parser.addoption(“—browser”)
[[[[[[[[
Pytest: Pytest is a testing framework for Python that allows you to write and run test code to verify the
correctness of your application.
pytest_adoption function: This function seems to be a hook that is used in Pytest plugins. Pytest allows you to
define plugins to extend its functionality, and hooks are specific points in the testing process where you can
inject custom behavior.
parser: The parser parameter is likely an instance of the argparse.ArgumentParser class. This class is part of the
Python Standard Library and is used for parsing command-line arguments.
parser.addoption(“—browser”): This line is adding a command-line option to the Pytest parser. The option
being added is --browser. Command-line options are used to pass additional information or configuration to
your test suite when you run it.
So, when you run your Pytest suite, you can now use the --browser option to specify some information related
to the browser. For example:
pytest --browser=chrome
This could be part of a testing setup where you want to run your tests in different browsers, and this option
allows you to dynamically configure the browser for your tests. The specifics of how this information is used
would depend on the rest of your testing code.
]]]]]]]]]]
@pytest.fixture()
Def browser(request):
return request.config.getoption(“—browser”)
[[[[[[[[
@pytest.fixture(): This is a decorator used in Pytest to define a fixture. Fixtures are functions that provide a
fixed baseline upon which your tests can depend.
def browser(request): This line declares a function named browser that takes a request parameter. In the
context of Pytest fixtures, the request object is automatically provided by Pytest and can be used to access
information about the current test run.
getoption("—browser") is used to retrieve the value of the --browser option that was passed when running the
tests.
So, when you use this fixture in your test functions, it will provide the value of the --browser option that you
specified on the command line.
]]]]]]]]]]
[[[[[[[[[[[[[[The @pytest.fixture(scope="session") decorator is used in Pytest to define a fixture with a specific
scope. Let's break it down:
@pytest.fixture: This is a decorator in Pytest used to define fixtures. Fixtures are functions that provide a
baseline setup and can be shared across multiple tests.
scope="session": This argument specifies the scope of the fixture. In this case, the fixture has a session scope.
The available scopes in Pytest are:
session: The fixture is invoked only once for the entire test session.
When a fixture has a session scope, it means that its setup code will run only once at the beginning of the test
session, and the teardown code (if any) will run once at the end of the test session. This can be useful for
setting up resources that are expensive to create or that need to be shared across multiple tests.
So, in the context of your code, @pytest.fixture(scope="session") is defining a fixture named driver_get that
will set up a Selenium WebDriver instance (in this case, for the Chrome browser) at the start of the test session
and close it at the end of the test session. This allows the WebDriver to be reused across multiple tests,
improving efficiency.]]]]]]]]]]]]]]]]]
@pytest.fixture(scope=”session”)
If browser == ‘chrome’:
options = webdriver.ChromeOptions()
options.add_experimental_option(“detach”,True)
driver = webdriver.Chrome(options=options)
print(“launching Chrome Browser”)
options=options()
driver = webdriver.Firefox(options=options)
driver = webdriver.Edge()
else:
options = webdriver.ChromeOptions()
options.add_argument(“headless”)
driver = webdriver.Chrome()
print(“headless mode”)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(“https://........//)
yield driver
driver.quite
Paramaterize pytest fixture( Conftest.py) --- for passing different inputs in login
### create fixture
@pytest.fixture(params = [
(“22suchi22@gmail.com”,”password”),
(“22priya22@gmail.com”,”password2”),
(“22akash@gmail.com”,”password3”)])
Def get_data_for_login(request):
return request.param