Best Python code snippet using pytest
conftest.py
Source:conftest.py
...85 # get driver and assign to fixtures class scope86 pytest.driver = init_driver(request.config.getoption("browser_name"))8788 # Updating Suite level information in junit xml89# record_testsuite_property("framework","Project made by Himanshu Aggarwal")90# record_testsuite_property("product",pytest.config['product'])91# record_testsuite_property("url",pytest.config['url'])92# record_testsuite_property("build_name",pytest.config['build_name'])93# record_testsuite_property("build_url",pytest.config['build_url'])94# #record_testsuite_property("environment",os.name)95# record_testsuite_property("environment",platform.platform())96# record_testsuite_property("target",request.config.getoption("browser_name"))97 98 #endregion99100 #region teardown 101 yield102 # quit the driver at the end of session103 pytest.driver.close()104105 #endregion106107def init_driver(browser_name):108 logging.info(f'creating driver for provided browser - {browser_name}')109 driver = webdriver.Chrome(executable_path='./drivers/chromedriver.exe')110 driver.maximize_window()
...
fixtures.py
Source:fixtures.py
...50 """51 env = request.config._labgrid_env52 if not env:53 pytest.skip("missing environment config (use --lg-env)")54 record_testsuite_property('ENV_CONFIG', env.config_file)55 targets = list(env.config.get_targets().keys())56 record_testsuite_property('TARGETS', targets)57 for target_name in targets:58 try:59 target = env.get_target(target_name)60 except UserError as e:61 pytest.exit(e)62 try:63 remote_place = target.get_resource(RemotePlace, wait_avail=False)64 remote_name = remote_place.name65 record_testsuite_property(66 f'TARGET_{target_name.upper()}_REMOTE', remote_name)67 except NoResourceFoundError:68 pass69 for name, path in env.config.get_paths().items():70 record_testsuite_property(f'PATH_{name.upper()}', path)71 try:72 sha = subprocess.check_output(73 "git rev-parse HEAD".split(), cwd=path, stderr=subprocess.DEVNULL)74 except subprocess.CalledProcessError:75 continue76 except FileNotFoundError:77 continue78 record_testsuite_property(79 f'PATH_{name.upper()}_GIT_COMMIT',80 sha.decode("utf-8").strip("\n"))81 for name, image in env.config.get_images().items():82 record_testsuite_property(83 f'IMAGE_{name.upper()}', image)84 try:85 sha = subprocess.check_output(86 "git rev-parse HEAD".split(), cwd=os.path.dirname(image),87 stderr=subprocess.DEVNULL)88 except subprocess.CalledProcessError:89 continue90 except FileNotFoundError:91 continue92 record_testsuite_property(93 f'IMAGE_{name.upper()}_GIT_COMMIT',94 sha.decode("utf-8").strip("\n"))95 yield env96 env.cleanup()97 sshmanager.close_all()98@pytest.fixture(scope="session")99def target(env):100 """Return the default target `main` configured in the supplied101 configuration file."""102 target = env.get_target()103 if target is None:104 raise UserError("Using target fixture without 'main' target in config")105 return target106@pytest.fixture(scope="session")...
test_advent3.py
Source:test_advent3.py
...3from adventofcode import advent34def test_advent3(user_data, user, record_testsuite_property):5 input_file = os.path.join(user_data, "3.txt")6 result = advent3.main1(input_file)7 record_testsuite_property(f'{user}-3-1', result)8 result = advent3.main2(input_file)9 record_testsuite_property(f'{user}-3-2', result)10def test_read_1_3_lines():11 input = [12 "0111111111",13 "1110111111",14 "1111110111",15 "1111111110",16 "1101111111",17 "1111101111",18 "1111111101",19 "1011111111",20 "1111011111",21 "1111111011",22 "0111111111",23 ]...
test_advent2.py
Source:test_advent2.py
...3from adventofcode import advent24def test_advent2(user_data, user, record_testsuite_property):5 file = os.path.join(user_data, '2.txt')6 result = advent2.main1(file)7 record_testsuite_property(f'{user}-2-1', result)8 result = advent2.main2(file)9 record_testsuite_property(f'{user}-2-2', result)10@pytest.mark.parametrize('input,expected', (11 ('1-3 a: abcde', True),12 ('1-3 b: cdefg', False),13 ('2-9 c: ccccccccc', True),14))15def test_check_old_validity(input, expected):16 assert advent2.check_old_validity(input) is expected17@pytest.mark.parametrize('input,expected', (18 ('1-3 a: abcde', (1,3,'a','abcde')),19 ('1-3 b: cdefg', (1,3,'b', 'cdefg')),20 ('2-9 c: ccccccccc', (2,9,'c','ccccccccc')),21))22def test_get_params(input, expected):23 assert advent2.get_params(input) == expected...
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!