Best Python code snippet using lisa_python
test.py
Source:test.py  
...52        output_file = open(self.output_file, 'a')53        print(text)54        output_file.write(text + "\n")55        output_file.close()56    def get_file_size(self,file_name):57        '''58        Function to get the file size of a file with its name59        '''60        with open(file_name, 'r') as file_obj:61            line_count = 062            for line_count,_ in enumerate(file_obj):63                continue64            file_obj.close()65            return line_count+166        67    def test_invalid_dir(self):68        '''69        Testing for a non existing directory70        '''71        #testing non existent directory        72        self.redirect_output_store() 73        test_dir = "./x/"74        main.get_dir_file_details(test_dir)                                   75        self.redirect_output_normal() 76        self.print_test_output("Testing Non-Existing Directory:")77        assert self.output_obj.getvalue().strip() == 'Invalid Directory'78        79    #test for empty dir80    def test_empty_dir(self):81        '''82        Testing for a empty directory83        '''84        #testing empty directory85        self.redirect_output_store()86        test_dir = "./empty"87        os.makedirs(test_dir, exist_ok=True)88        main.get_dir_file_details(test_dir)89        self.redirect_output_normal() 90        self.print_test_output("Testing Empty Directory:")91        assert self.output_obj.getvalue().strip() == 'No Files Present in Directory'92        os.removedirs(test_dir)93        94    #test for single empty file95    def test_single_empty_file(self):96        '''97        Testing for a single empty file98        '''99        #testing empty directory100        self.redirect_output_store()101        test_dir = "./single-file-empty"102        # creatinga. directory with a file in it103        os.makedirs(test_dir, exist_ok=True)104        with open(os.path.join(test_dir,'file1.txt'), 'w') as file_object:105            file_object.close()106        main.get_dir_file_details(test_dir)107        self.redirect_output_normal() 108        self.print_test_output("Testing Single Empty File:")109        self.assertIn(os.path.join(test_dir,'file1.txt'), self.output_obj.getvalue().strip())110        #check number of lines = 1 for emptyfile111        #print(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip())112        assert int(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip()) == 1113        shutil.rmtree(test_dir)114        115    #test for multiple empty files116    def test_multiple_empty_file(self):117        '''118        Testing for a multiple empty files119        '''120        #testing empty directory121        self.redirect_output_store()122        test_dir = "./multiple-file-empty"123        # creating directory with a random number of files in it124        os.makedirs(test_dir, exist_ok=True)125        rand_file_num = random.randrange(2,10) 126        for i in range(1,rand_file_num):127            with open(os.path.join(test_dir,f'file_{i}.txt'), 'w') as file_object:128                file_object.close()129        main.get_dir_file_details(test_dir)130        self.redirect_output_normal() 131        self.print_test_output("Testing Multiple Empty Files:")132        self.assertIn(os.path.join(test_dir,'file_1.txt'), self.output_obj.getvalue().strip())133        #check number of lines = 1 for emptyfile134        #print(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip())135        assert int(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip()) == 1136        last_file_name = os.path.join(test_dir,f'file_{int(rand_file_num)-1}.txt')137        self.assertIn(last_file_name, self.output_obj.getvalue().strip())138        assert int(self.output_obj.getvalue().split(last_file_name)[1].split("\n")[0].strip()) == 1139        shutil.rmtree(test_dir)140        141    #test for multiple small files142    def test_multiple_small_files(self):143        '''144        Testing for a multiple empty files145        '''146        #testing empty directory147        self.redirect_output_store()148        test_dir = "./multiple-file-small"149        os.makedirs(test_dir, exist_ok=True)150        rand_file_num = random.randrange(2,10) 151        for i in range(1,rand_file_num):152            with open(os.path.join(test_dir,f'file_{i}.txt'), 'w') as file_object:153                for line_count in range(random.randrange(1000,5000)):154                    file_object.write(f"test line{line_count}")155                    file_object.write("\n")156                file_object.close()157        main.get_dir_file_details(test_dir)158        self.redirect_output_normal() 159        self.print_test_output("Testing Multiple Small Files:")160        # check the size of the first and the last file and check if it is present in output161        self.assertIn(os.path.join(test_dir,'file_1.txt'), self.output_obj.getvalue().strip())162        file_size = self.get_file_size(os.path.join(test_dir,'file_1.txt'))163        assert int(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip()) == file_size164        last_file_name = os.path.join(test_dir,f'file_{int(rand_file_num)-1}.txt')165        last_file_size = self.get_file_size(last_file_name)166        self.assertIn(last_file_name, self.output_obj.getvalue().strip())167        assert int(self.output_obj.getvalue().split(last_file_name)[1].split("\n")[0].strip()) == last_file_size168        shutil.rmtree(test_dir)169    #test for multiple large files170    def test_multiple_large_files(self):171        '''172        Testing for a multiple empty files173        '''174        #testing empty directory175        self.redirect_output_store()176        test_dir = "./multiple-large-small"177        os.makedirs(test_dir, exist_ok=True)178        rand_file_num = random.randrange(2,10) 179        for i in range(1,rand_file_num):180            with open(os.path.join(test_dir,f'file_{i}.txt'), 'w') as file_object:181                for line_count in range(random.randrange(10000,50000)):182                    file_object.write(f"test line{line_count}")183                    file_object.write("\n")184                file_object.close()185        main.get_dir_file_details(test_dir)186        self.redirect_output_normal() 187        self.print_test_output("Testing Multiple Large Files:")188        # check the size of the first and the last file and check if it is present in output189        self.assertIn(os.path.join(test_dir,'file_1.txt'), self.output_obj.getvalue().strip())190        #check number of lines = 1 for emptyfile191        #print(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip())192        file_size = self.get_file_size(os.path.join(test_dir,'file_1.txt'))193        assert int(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip()) == file_size194        last_file_name = os.path.join(test_dir,f'file_{int(rand_file_num)-1}.txt')195        last_file_size = self.get_file_size(last_file_name)196        self.assertIn(last_file_name, self.output_obj.getvalue().strip())197        assert int(self.output_obj.getvalue().split(last_file_name)[1].split("\n")[0].strip()) == last_file_size198        shutil.rmtree(test_dir)199        200    #test for multiple very large files201    def test_multiple_very_large_files(self):202        '''203        Testing for a multiple empty files204        '''205        #testing empty directory206        self.redirect_output_store()207        test_dir = "./multiple-very-large-small"208        os.makedirs(test_dir, exist_ok=True)209        rand_file_num = random.randrange(2,10) 210        for i in range(1,rand_file_num):211            with open(os.path.join(test_dir,f'file_{i}.txt'), 'w') as file_object:212                for line_count in range(random.randrange(1000000,5000000)):213                    file_object.write(f"test line{line_count}")214                    file_object.write("\n")215                file_object.close()216        main.get_dir_file_details(test_dir)217        self.redirect_output_normal() 218        self.print_test_output("Testing Multiple Very Large Files:")219        extension=".txt"220        filename = os.path.join(test_dir,f'file_1{extension}')221        self.assertIn(filename, self.output_obj.getvalue().strip())222        #check number of lines = 1 for emptyfile223        #print(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip())224        file_size = self.get_file_size(os.path.join(test_dir,'file_1.txt'))225        assert int(self.output_obj.getvalue().split(filename)[1].split("\n")[0].strip()) == file_size226        last_file_name = os.path.join(test_dir,f'file_{int(rand_file_num)-1}.txt')227        last_file_size = self.get_file_size(last_file_name)228        self.assertIn(last_file_name, self.output_obj.getvalue().strip())229        assert int(self.output_obj.getvalue().split(last_file_name)[1].split("\n")[0].strip()) == last_file_size230        shutil.rmtree(test_dir)231        232    #test for multiple small python files233    def test_multiple_small_python_files(self):234        '''235        Testing for a multiple small python files236        '''237        #testing empty directory238        self.redirect_output_store()239        test_dir = "./multiple-file-small-python"240        os.makedirs(test_dir, exist_ok=True)241        rand_file_num = random.randrange(2,20) 242        extension = ".py"243        for i in range(1,rand_file_num):244            with open(os.path.join(test_dir,f'file_{i}{extension}'), 'w') as file_object:245                for line_count in range(random.randrange(1000,5000)):246                    file_object.write(f"test line{line_count}")247                    file_object.write("\n")248                file_object.close()249        main.get_dir_file_details(test_dir, filename_extension=extension)250        self.redirect_output_normal() 251        self.print_test_output("Testing Multiple Small Python Files:")252        filename = os.path.join(test_dir,f'file_1{extension}')253        self.assertIn(filename, self.output_obj.getvalue().strip())254        #check number of lines = 1 for emptyfile255        #print(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip())256        file_size = self.get_file_size(os.path.join(test_dir,f'file_1{extension}'))257        assert int(self.output_obj.getvalue().split(filename)[1].split("\n")[0].strip()) == file_size258        last_file_name = os.path.join(test_dir,f'file_{int(rand_file_num)-1}{extension}')259        last_file_size = self.get_file_size(last_file_name)260        self.assertIn(last_file_name, self.output_obj.getvalue().strip())261        assert int(self.output_obj.getvalue().split(last_file_name)[1].split("\n")[0].strip()) == last_file_size262        shutil.rmtree(test_dir)263        264    #test for multiple small excel files265    def test_multiple_small_excel_files(self):266        '''267        Testing for a multiple small excel files268        '''269        #testing empty directory270        self.redirect_output_store()271        test_dir = "./multiple-file-small-excel"272        os.makedirs(test_dir, exist_ok=True)273        rand_file_num = random.randrange(2,30) 274        extension = ".xlsx"275        for i in range(1,rand_file_num):276            with open(os.path.join(test_dir,f'file_{i}{extension}'), 'w') as file_object:277                for line_count in range(random.randrange(1000,5000)):278                    file_object.write(f"test line{line_count}")279                    file_object.write("\n")280                file_object.close()281        main.get_dir_file_details(test_dir, filename_extension=extension)282        self.redirect_output_normal() 283        self.print_test_output("Testing Multiple Small Excel Files:")284        filename = os.path.join(test_dir,f'file_1{extension}')285        self.assertIn(filename, self.output_obj.getvalue().strip())286        #check number of lines = 1 for emptyfile287        #print(self.output_obj.getvalue().split(" ")[1].split("\n")[0].strip())288        file_size = self.get_file_size(os.path.join(test_dir,f'file_1{extension}'))289        assert int(self.output_obj.getvalue().split(filename)[1].split("\n")[0].strip()) == file_size290        last_file_name = os.path.join(test_dir,f'file_{int(rand_file_num)-1}{extension}')291        last_file_size = self.get_file_size(last_file_name)292        self.assertIn(last_file_name, self.output_obj.getvalue().strip())293        assert int(self.output_obj.getvalue().split(last_file_name)[1].split("\n")[0].strip()) == last_file_size294        shutil.rmtree(test_dir)295        296    #test for multiple small doc files297    def test_multiple_small_doc_files(self):298        '''299        Testing for a multiple small doc files300        '''301        #testing empty directory302        self.redirect_output_store()303        test_dir = "./multiple-file-small-doc"304        os.makedirs(test_dir, exist_ok=True)305        rand_file_num = random.randrange(2,30) 306        extension = ".docx"307        for i in range(1,rand_file_num):308            with open(os.path.join(test_dir,f'file_{i}{extension}'), 'w') as file_object:309                for line_count in range(random.randrange(1000,5000)):310                    file_object.write(f"test line{line_count}")311                    file_object.write("\n")312                file_object.close()313        main.get_dir_file_details(test_dir, filename_extension=extension)314        self.redirect_output_normal() 315        self.print_test_output("Testing Multiple Small Doc Files:")316        #self.print("output chekerrrr:")317        filename = os.path.join(test_dir,f'file_1{extension}')318        #self.print()319        self.assertIn(filename, self.output_obj.getvalue().strip())320        #check number of lines = 1 for emptyfile321        322        file_size = self.get_file_size(filename)323        assert int(self.output_obj.getvalue().split(filename)[1].split("\n")[0].strip()) == file_size324        last_file_name = os.path.join(test_dir,f'file_{int(rand_file_num)-1}{extension}')325        last_file_size = self.get_file_size(last_file_name)326        self.assertIn(last_file_name, self.output_obj.getvalue().strip())327        assert int(self.output_obj.getvalue().split(last_file_name)[1].split("\n")[0].strip()) == last_file_size328        shutil.rmtree(test_dir)329    #test for multiple empty files330    def test_non_existing_extension(self):331        '''332        Testing for a multiple empty files333        '''334        #testing empty directory335        self.redirect_output_store()336        test_dir = "./multiple-file-empty"337        os.makedirs(test_dir, exist_ok=True)338        rand_file_num = random.randrange(2,10) 339        for i in range(1,rand_file_num):340            with open(os.path.join(test_dir,'file_{i}.txt'), 'w') as file_object:341                file_object.close()342        main.get_dir_file_details(test_dir, filename_extension=".py")343        self.redirect_output_normal() 344        self.print_test_output("Testing Empty Directory:")345        assert self.output_obj.getvalue().strip() == 'No Files Present in Directory'346        shutil.rmtree(test_dir)347    def create_dirs(self, test_dir, count=4):348        '''349        Function to create multiple level directories using recursion350        '''351        os.makedirs(test_dir, exist_ok=True)352        rand_file_num = random.randrange(2,7)353        count = count+1354        for i in range(1,rand_file_num):355            random_number_dir = random.randint(19,820)356            if random_number_dir%2==0 and count<10:357                count = count+1358                self.create_dirs(os.path.join(test_dir, str(random_number_dir)), count)359            if count>10:360                return361            with open(os.path.join(test_dir,f'file_{i}.txt'), 'w') as file_object:362                if i%2 == 0:363                    for line_count in range(random.randrange(1000,5000)):364                        file_object.write(f"test line{line_count}")365                        file_object.write("\n")366                file_object.close()367    #multiple level directories368    def test_multiple_level_directories(self):369        '''370        Testing for a multiple level directories371        '''372        #testing multiple directory levels373        self.redirect_output_store()374        test_dir = "./multiple-level-directory"375        self.create_dirs(test_dir, count=0)376        main.get_dir_file_details(test_dir, filename_extension=".py")377        self.redirect_output_normal() 378        self.print_test_output("Testing Multiple Level Directory:")379        file_name = self.output_obj.getvalue().strip().split(" ")[0]380        file_size = self.get_file_size(file_name)381        assert int(self.output_obj.getvalue().split(file_name)[1].split("\n")[0].strip()) == file_size382        shutil.rmtree(test_dir)383    ...test_file_completeness.py
Source:test_file_completeness.py  
...51    def test_requirements_exists(self):52        _file = ROOT_DIR.joinpath('requirements.txt')53        self.assertEqual(_file.exists(), True)54        self.assertEqual(_file.is_dir(), False)55        self.assertNotEqual(get_file_size(_file), 0)56    def test_license_exists(self):57        _file = ROOT_DIR.joinpath('LICENSE')58        self.assertEqual(_file.exists(), True)59        self.assertEqual(_file.is_dir(), False)60        self.assertNotEqual(get_file_size(_file), 0)61    def test_readme_exists(self):62        _file = ROOT_DIR.joinpath('README.md')63        self.assertEqual(_file.exists(), True)64        self.assertEqual(_file.is_dir(), False)65        self.assertNotEqual(get_file_size(_file), 0)66    def test_win_build_exists(self):67        _file = ROOT_DIR.joinpath('win_build.bat')68        self.assertEqual(_file.exists(), True)69        self.assertEqual(_file.is_dir(), False)70        self.assertNotEqual(get_file_size(_file), 0)71    def test_linux_build_exists(self):72        _file = ROOT_DIR.joinpath('linux_build.sh')73        self.assertEqual(_file.exists(), True)74        self.assertEqual(_file.is_dir(), False)75        self.assertNotEqual(get_file_size(_file), 0)76    def test_disclaimer_exists(self):77        _file = ROOT_DIR.joinpath('DISCLAIMER.md')78        self.assertEqual(_file.exists(), True)79        self.assertEqual(_file.is_dir(), False)80        self.assertNotEqual(get_file_size(_file), 0)81    def test_version_file_valid(self):82        _file = ROOT_DIR.joinpath('version')83        self.assertEqual(_file.exists(), True)84        self.assertEqual(_file.is_dir(), False)85        self.assertNotEqual(get_file_size(_file), 0)86        with open(_file, 'r') as f:87            ver = f.read()88        self.assertEqual(ver, get_app_version())89        self.assertEqual(type(version.parse(ver)), version.Version)90    def test_banner_valid(self):91        _file = ROOT_DIR.joinpath('assets', 'banner.png')92        self.assertEqual(_file.exists(), True)93        self.assertNotEqual(get_file_size(_file), 0)94    def test_code_of_conduct_valid(self):95        _file = ROOT_DIR.joinpath('.github', 'CODE_OF_CONDUCT.md')96        self.assertEqual(_file.exists(), True)97        self.assertNotEqual(get_file_size(_file), 0)98    def test_pull_request_template_valid(self):99        _file = ROOT_DIR.joinpath('.github', 'PULL_REQUEST_TEMPLATE.md')100        self.assertEqual(_file.exists(), True)101        self.assertNotEqual(get_file_size(_file), 0)102    def test_funding_valid(self):103        _file = ROOT_DIR.joinpath('.github', 'FUNDING.yml')104        self.assertEqual(_file.exists(), True)105        self.assertNotEqual(get_file_size(_file), 0)106    def test_workflows_valid(self):107        _file = ROOT_DIR.joinpath('.github', 'workflows', 'run-pytest.yml')108        self.assertEqual(_file.exists(), True)109        self.assertNotEqual(get_file_size(_file), 0)110        _file = ROOT_DIR.joinpath('.github', 'workflows', 'wgv-pyinstaller.yml')111        self.assertEqual(_file.exists(), True)112        self.assertNotEqual(get_file_size(_file), 0)113        _file = ROOT_DIR.joinpath('.github', 'workflows', 'labeler.yml')114        self.assertEqual(_file.exists(), True)115        self.assertNotEqual(get_file_size(_file), 0)116    def test_issue_template_valid(self):117        _file = ROOT_DIR.joinpath('.github', 'ISSUE_TEMPLATE', 'bug_report.md')118        self.assertEqual(_file.exists(), True)119        self.assertNotEqual(get_file_size(_file), 0)120        _file = ROOT_DIR.joinpath('.github', 'ISSUE_TEMPLATE', 'feature_request.md')121        self.assertEqual(_file.exists(), True)122        self.assertNotEqual(get_file_size(_file), 0)123if __name__ == '__main__':...test_monitor.py
Source:test_monitor.py  
1# coding: utf-82import unittest3from pytddmon import Monitor4class TestChangeDetection(unittest.TestCase):5    def _set_up_monitor(self):6        files = ['file']7        file_finder = lambda: files8        get_file_size = lambda x: 19        get_file_modification_time = lambda x: 110        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)11        return files, monitor12    def test_modification_time_changed(self):13        files = ['file']14        file_finder = lambda: files15        get_file_size = lambda x: 116        modtime = [1]17        get_file_modification_time = lambda x: modtime[0]18        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)19        modtime[0] = 220        change_detected = monitor.look_for_changes()21        assert change_detected22    def test_nothing_changed(self):23        files, monitor = self._set_up_monitor()24        change_detected = monitor.look_for_changes()25        assert not change_detected26    def test_adding_file(self):27        files, monitor = self._set_up_monitor()28        files.append('file2')29        change_detected = monitor.look_for_changes()30        assert change_detected31    def test_renaming_file(self):32        files, monitor = self._set_up_monitor()33        files[0] = 'renamed'34        change_detected = monitor.look_for_changes()35        assert change_detected36    def test_change_is_only_detected_once(self):37        files, monitor = self._set_up_monitor()38        files[0] = 'changed'39        change_detected = monitor.look_for_changes()40        change_detected = monitor.look_for_changes()41        assert not change_detected42    def test_file_size_changed(self):43        files = ['file']44        filesize = [1]45        file_finder = lambda: files46        get_file_size = lambda x: filesize[0]47        get_file_modification_time = lambda x: 148        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)49        filesize[0] = 550        change_detected = monitor.look_for_changes()51        assert change_detected52    def test_file_order_does_not_matter(self):53        files = ['file', 'file2']54        file_finder = lambda: files55        get_file_size = lambda x: 156        get_file_modification_time = lambda x: 157        monitor = Monitor(file_finder, get_file_size, get_file_modification_time)58        files[:] = ['file2', 'file']59        change_detected = monitor.look_for_changes()60        assert not change_detected61if __name__ == '__main__':...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!!
