Best Python code snippet using avocado_python
test_f1.py
Source:test_f1.py  
1import unittest as ut2from unittest import mock3from f1 import F14from resource import Resource5import datetime6import requests7class TestF1(ut.TestCase):8    @mock.patch('requests.get', autospec=True)9    def test_driver_standings_ok(self, mock_response):10        f1 = F1()11        mock_response.return_value.status_code = 20012        test_json = {'MRData':{'StandingsTable':{'StandingsLists':[{'DriverStandings':[13            {'position':'1', 'points':'13', 'Driver':{'familyName':'Hamilton'}},14            {'position':'2', 'points':'8', 'Driver':{'familyName':'Verstappen'}},15            {'position':'3', 'points':'8', 'Driver':{'familyName':'Bottas'}}16        ]}]}}}17        mock_response.return_value.json.return_value = test_json18        19        expected_standings = [('1', '13', 'Hamilton'), ('2', '8', 'Verstappen'), ('3', '8', 'Bottas')]20        standings = f1.driver_standings()21        self.assertEqual(expected_standings, standings)22    @mock.patch('requests.get', autospec=True)23    def test_driver_standings_empty(self, mock_response):24        f1 = F1()25        mock_response.return_value.status_code = 40326        test_json = {}27        mock_response.return_value.json.return_value = test_json28        29        expected_standings = []30        standings = f1.driver_standings()31        self.assertEqual(expected_standings, standings)32    @mock.patch('requests.get', autospec=True)33    def test_driver_standings_key_error(self, mock_response):34        f1 = F1()35        mock_response.return_value.status_code = 20036        test_json = {'MRData':{'StandingsTable':{'InvalidKey':[{'DriverStandings':[37            {'position':'1', 'points':'13', 'Driver':{'familyName':'Hamilton'}},38            {'position':'2', 'points':'8', 'Driver':{'familyName':'Verstappen'}},39            {'position':'3', 'points':'8', 'Driver':{'familyName':'Bottas'}}40        ]}]}}}41        mock_response.return_value.json.return_value = test_json42        43        expected_standings = []44        standings = f1.driver_standings()45        self.assertEqual(expected_standings, standings)46    @mock.patch('requests.get', autospec=True)47    def test_driver_standings_index_error(self, mock_response):48        f1 = F1()49        mock_response.return_value.status_code = 20050        test_json = {'MRData':{'StandingsTable':{'StandingsLists':[]}}}51        mock_response.return_value.json.return_value = test_json52        expected_standings = []53        standings = f1.constructor_standings()54        self.assertEqual(expected_standings, standings)55    @mock.patch('requests.get', autospec=True)56    def test_constructor_standings_ok(self, mock_response):57        f1 = F1()58        mock_response.return_value.status_code = 20059        test_json = {'MRData':{'StandingsTable':{'StandingsLists':[{'ConstructorStandings':[60            {'position':'1', 'points':'13', 'Constructor':{'name':'Mercedes'}},61            {'position':'2', 'points':'8', 'Constructor':{'name':'Redbull'}},62            {'position':'3', 'points':'8', 'Constructor':{'name':'Alfa Romeo'}}63        ]}]}}}64        mock_response.return_value.json.return_value = test_json65        expected_standings = [('1', '13', 'Mercedes'), ('2', '8', 'Redbull'), ('3', '8', 'Alfa Romeo')]66        standings = f1.constructor_standings()67        self.assertEqual(expected_standings, standings)68    @mock.patch('requests.get', autospec=True)69    def test_constructor_standings_empty(self, mock_response):70        f1 = F1()71        mock_response.return_value.status_code = 40472        test_json = {}73        mock_response.return_value.json.return_value = test_json74        expected_standings = []75        standings = f1.constructor_standings()76        self.assertEqual(expected_standings, standings)77    @mock.patch('requests.get', autospec=True)78    def test_constructor_standings_key_error(self, mock_response):79        f1 = F1()80        mock_response.return_value.status_code = 20081        test_json = {'MRData':{'InvalidKey':{'StandingsLists':[{'ConstructorStandings':[82            {'position':'1', 'points':'13', 'Constructor':{'name':'Mercedes'}},83            {'position':'2', 'points':'8', 'Constructor':{'name':'Redbull'}},84            {'position':'3', 'points':'8', 'Constructor':{'name':'Alfa Romeo'}}85        ]}]}}}86        mock_response.return_value.json.return_value = test_json87        expected_standings = []88        standings = f1.constructor_standings()89        self.assertEqual(expected_standings, standings)90    @mock.patch('requests.get', autospec=True)91    def test_constructor_standings_index_error(self, mock_response):92        f1 = F1()93        mock_response.return_value.status_code = 20094        test_json = {'MRData':{'StandingsTable':{'StandingsLists':[]}}}95        mock_response.return_value.json.return_value = test_json96        expected_standings = []97        standings = f1.constructor_standings()98        self.assertEqual(expected_standings, standings)99    @mock.patch('requests.get', autospec=True)100    def test_season_ok(self, mock_response):101        f1 = F1()102        mock_response.return_value.status_code = 200103        test_json = {'MRData':{'RaceTable':{'Races':[104            {'date':'2022-07-20', 'raceName':'Belgian Grand Prix'},105            {'date':'2022-06-13', 'raceName':'German Grand Prix'}106        ]}}}107        mock_response.return_value.json.return_value = test_json108        expected_standings = [('20.07.2022', 'Belgian Grand Prix'), ('13.06.2022', 'German Grand Prix')]109        standings = f1.season()110        self.assertEqual(expected_standings, standings)111    @mock.patch('requests.get', autospec=True)112    def test_season_key_error(self, mock_response):113        f1 = F1()114        mock_response.return_value.status_code = 200115        test_json = {'MRData':{'InvalidKey':{'Races':[116            {'date':'2022-07-20', 'raceName':'Belgian Grand Prix'},117            {'date':'2022-06-13', 'raceName':'German Grand Prix'}118        ]}}}119        mock_response.return_value.json.return_value = test_json120        expected_standings = []121        standings = f1.season()122        self.assertEqual(expected_standings, standings)123    @mock.patch('requests.get', autospec=True)124    def test_season_empty_02(self, mock_response):125        f1 = F1()126        mock_response.return_value.status_code = 200127        test_json = {'MRData':{'RaceTable':{'Races':[128        ]}}}129        mock_response.return_value.json.return_value = test_json130        expected_standings = []131        standings = f1.season()132        self.assertEqual(expected_standings, standings)133if __name__ == '__main__':...test_settings_class.py
Source:test_settings_class.py  
1from pathlib import Path2from unittest import TestCase3from config import UnknownFileExtension4from config import Settings5import settings_test as st6class SettingsClass(TestCase):7    def test_settings_creation_without_config_files(self):8        settings_instance = Settings('settings_test')9        self.assertEqual(st.SOME_VAR, settings_instance.SOME_VAR)10        self.assertEqual(st.dict_const, settings_instance.dict_const)11        self.assertEqual(st.another_var, settings_instance.another_var)12    def test_settings_creation_with_json_config(self):13        settings_instance = Settings('settings_test', {'test_json': Path(__file__).parent / 'test_config.json'})14        self.assertTrue(settings_instance.test_json)15        self.assertEqual(settings_instance.test_json['test_var_1'], 1)16        self.assertEqual(settings_instance.test_json['test_var_2'], 'test_var_2 value')17        self.assertEqual(settings_instance.test_json['test_var_3'], 3)18        self.assertEqual(settings_instance.test_json['dict_var']['one'], 1)19        self.assertRaises(KeyError, lambda: settings_instance.test_json['dict_var']['not existed var'])20    def test_unknown_file_extension(self):21        file_extensions = ['s', 'gg', 's', 'mp3', 'son', 'i']22        for extension in file_extensions:23            self.assertRaises(24                UnknownFileExtension, lambda: Settings(25                    'settings_test',26                    {'test_json': Path(__file__).parent / f'test_config.{extension}'}27                )28            )29    def test_settings_creation_with_json_config_and_string_path(self):30        settings_instance = Settings(31            'settings_test',32            {'test_json': str(Path(__file__).parent / 'test_config.json')}33        )34        self.assertTrue(settings_instance.test_json)35        self.assertEqual(settings_instance.test_json['test_var_1'], 1)36        self.assertEqual(settings_instance.test_json['test_var_2'], 'test_var_2 value')37        self.assertEqual(settings_instance.test_json['test_var_3'], 3)38        self.assertEqual(settings_instance.test_json['dict_var']['one'], 1)39        self.assertRaises(KeyError, lambda: settings_instance.test_json['dict_var']['not existed var'])40    def test_settings_creation_with_multiple_json_files(self):41        settings_instance = Settings(42            'settings_test',43            {44                'test_json': str(Path(__file__).parent / 'test_config.json'),45                'someatr': str(Path(__file__).parent / 'test_config2.json'),46                'someatr2': str(Path(__file__).parent / 'test_config2.json'),47            }48        )49        self.assertTrue(settings_instance.test_json)50        self.assertTrue(settings_instance.someatr)51        self.assertTrue(settings_instance.someatr2)52    def test_settings_creation_with_ini_files(self):53        settings_instance = Settings(54            'settings_test',55            {56                'ini': str(Path(__file__).parent / 'test_ini_config.ini'),57                'ini2': str(Path(__file__).parent / 'test_ini_config.ini'),58                'ini3': str(Path(__file__).parent / 'test_ini_config.ini'),59            }60        )61        self.assertTrue(settings_instance.ini)62        self.assertTrue(settings_instance.ini2)63        self.assertTrue(settings_instance.ini3)64        self.assertEqual(settings_instance.ini['INI']['var'], '2')65        self.assertEqual(settings_instance.ini2['INI']['not number'], 'word')66        self.assertEqual(settings_instance.ini2['INI']['number'], 'some interesting number')67    def test_settings_creation_with_multiple_ini_files(self):68        settings_instance = Settings(69            'settings_test',70            {71                'ini': str(Path(__file__).parent / 'test_ini_config.ini'),72                'cfg': str(Path(__file__).parent / 'test_cfg_file.cfg'),73            }74        )75        self.assertTrue(settings_instance.ini)76        self.assertTrue(settings_instance.cfg)77        self.assertEqual(settings_instance.cfg['CFG']['cfg var'], '1')78        self.assertEqual(settings_instance.cfg['CFG']['not cfg number'], 'cfg')79        self.assertRaises(KeyError, lambda: settings_instance.cfg['CFG']['var'])80        self.assertEqual(settings_instance.ini['INI']['var'], '2')81        self.assertEqual(settings_instance.ini['INI']['not number'], 'word')82        self.assertEqual(settings_instance.ini['INI']['number'], 'some interesting number')83    def test_equal_settings_and_config_file_names(self):84        self.assertRaises(AttributeError, lambda: Settings(85            'settings_test',86            {'another_var': str(Path(__file__).parent / 'test_ini_config.ini')}87        ))88    def test_setup_config_files_after_creating_an_instance_of_the_class(self):89        settings_instance = Settings('settings_test')90        self.assertTrue(settings_instance)91        self.assertRaises(AttributeError, lambda: settings_instance.ini)92        self.assertRaises(AttributeError, lambda: settings_instance.test_json)93        self.assertRaises(AttributeError, lambda: settings_instance.cfg)94        settings_instance.setup_config_files({95            'ini': Path(__file__).parent / 'test_ini_config.ini',96            'cfg': Path(__file__).parent / 'test_cfg_file.cfg',97            'test_json': Path(__file__).parent / 'test_config.json',98        })99        self.assertTrue(settings_instance.ini)100        self.assertTrue(settings_instance.test_json)101        self.assertTrue(settings_instance.cfg)102        self.assertEqual(settings_instance.cfg['CFG']['cfg var'], '1')103        self.assertEqual(settings_instance.cfg['CFG']['not cfg number'], 'cfg')104        self.assertRaises(KeyError, lambda: settings_instance.cfg['CFG']['var'])105        self.assertEqual(settings_instance.ini['INI']['var'], '2')106        self.assertEqual(settings_instance.ini['INI']['not number'], 'word')107        self.assertEqual(settings_instance.ini['INI']['number'], 'some interesting number')108        self.assertTrue(settings_instance.test_json)109        self.assertEqual(settings_instance.test_json['test_var_1'], 1)110        self.assertEqual(settings_instance.test_json['test_var_2'], 'test_var_2 value')111        self.assertEqual(settings_instance.test_json['test_var_3'], 3)112        self.assertEqual(settings_instance.test_json['dict_var']['one'], 1)...test_add_product.py
Source:test_add_product.py  
1import pytest2from contextlib import nullcontext as does_not_raise3from scraper.filemanager import Filemanager4from scraper.add_product import add_product5from scraper.exceptions import WebsiteNotSupported6test_objects_json = Filemanager.read_json("./tests/test_objects.json")7test_json = test_objects_json["test_website_handlers"]8test_domains = [9    (test_json["amazon"]["link"], does_not_raise()),10    (test_json["ebay_with_itm"]["link"], does_not_raise()),11    (test_json["ebay_with_p"]["link"], does_not_raise()),12    (test_json["komplett"]["link"], does_not_raise()),13    (test_json["proshop"]["link"], does_not_raise()),14    (test_json["computersalg"]["link"], does_not_raise()),15    (test_json["elgiganten"]["link"], does_not_raise()),16    (test_json["avxperten"]["link"], does_not_raise()),17    (test_json["av-cables"]["link"], does_not_raise()),18    (test_json["power"]["link"], does_not_raise()),19    (test_json["expert"]["link"], does_not_raise()),20    (test_json["mm-vision"]["link"], does_not_raise()),21    (test_json["coolshop"]["link"], does_not_raise()),22    (test_json["sharkgaming"]["link"], does_not_raise()),23    (test_json["newegg"]["link"], does_not_raise()),24    (test_json["hifiklubben"]["link"], does_not_raise()),25    ("https://www.notsupported.com/", pytest.raises(WebsiteNotSupported)),26]27# Tests to make sure the websites that are supported can be added to be scraped28@pytest.mark.parametrize("url,expectation", test_domains)29def test_add_product(url, expectation, mocker):30    mocker.patch("scraper.Scraper.scrape_info", return_value=None)31    mocker.patch("scraper.Scraper.save_info", return_value=None)32    mocker.patch("scraper.filemanager.Filemanager.add_product_to_csv", return_value=None)33    mocker.patch("scraper.add_product.check_if_product_exists", return_value=False)34    mocker.patch("scraper.add_product.check_if_product_exists_csv", return_value=False)35    mocker.patch("scraper.add_product.add_product_to_records", return_value=None)36    with expectation:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
