Best Python code snippet using assertpy_python
tests.py
Source:tests.py
1import os2import unittest3import tempfile4import PyPDF25from click.testing import CliRunner6from pdfcli import cli7TEST_PDF_FOLDER = "test_files"8TEST_PDF_FILENAMES = ['PDF1.pdf', 'PDF2.pdf', 'PDF3.pdf']9TEST_PDF_PATHS = [os.path.join(TEST_PDF_FOLDER, file_name)10 for file_name in TEST_PDF_FILENAMES]11class BasePDFCLITestCase(unittest.TestCase):12 def setUp(self):13 self.test_pdf_file_handles = [open(path, 'rb') for path in TEST_PDF_PATHS]14 self.runner = CliRunner()15 def tearDown(self):16 for file_handle in self.test_pdf_file_handles:17 file_handle.close()18 if os.path.exists('test_files/out.pdf'):19 os.remove('test_files/out.pdf')20 if os.path.exists('out.pdf'):21 os.remove('out.pdf')22 if os.path.exists('out1.pdf'):23 os.remove('out1.pdf')24 if os.path.exists('out2.pdf'):25 os.remove('out2.pdf')26class TestPYPDF2(BasePDFCLITestCase):27 def test_pypdf2_merge(self):28 merger = PyPDF2.merger.PdfFileMerger()29 for file in self.test_pdf_file_handles:30 merger.append(file)31 self.assertEqual(len(merger.pages), 3)32 fp = tempfile.TemporaryFile()33 merger.write(fp)34 fp.seek(0)35 pdf_reader = PyPDF2.PdfFileReader(fp)36 self.assertEqual(pdf_reader.getNumPages(), 3)37 fp.close()38 def test_pypdf2_writer(self):39 reader_pdf1 = PyPDF2.PdfFileReader(self.test_pdf_file_handles[0])40 writer = PyPDF2.PdfFileWriter()41 writer.addPage(reader_pdf1.getPage(0))42 fp = tempfile.TemporaryFile()43 writer.write(fp)44 fp.seek(0)45 pdf_reader = PyPDF2.PdfFileReader(fp)46 self.assertEqual(pdf_reader.getNumPages(), 1)47class TestCLITool(BasePDFCLITestCase):48 def test_help_gets_called(self):49 result = self.runner.invoke(cli, ['--help'])50 self.assertEqual(result.exit_code, 0)51class TestMerge(BasePDFCLITestCase):52 def test_merge_valid_input(self):53 result = self.runner.invoke(cli, ['merge', '--out', 'test_files/out.pdf'] + TEST_PDF_PATHS)54 self.assertEqual(result.exit_code, 0)55 with open('test_files/out.pdf', 'rb') as file_reader:56 merged_pdf = PyPDF2.PdfFileReader(file_reader)57 self.assertEqual(merged_pdf.getNumPages(), 3)58 def test_merge_invalid_path(self):59 result = self.runner.invoke(cli, ['merge', 'fake_path'])60 self.assertEqual(result.exit_code, 2)61 def test_merge_valid_path_not_pdf(self):62 result = self.runner.invoke(cli, ['merge', 'test_files/test.txt', 'test_files/PDF1.pdf'])63 self.assertEqual(result.exit_code, 2)64 def test_merge_no_files_provided(self):65 result = self.runner.invoke(cli, ['merge'])66 self.assertEqual(result.exit_code, 2)67class TestReorder(BasePDFCLITestCase):68 def test_reorder_valid_input_reverse(self):69 result = self.runner.invoke(cli, ['reorder', 'test_files/MultiPagePDF.pdf',70 '--out', 'test_files/out.pdf', '--reverse'])71 self.assertEqual(result.exit_code, 0)72 def test_reorder_valid_input_order_specified(self):73 result = self.runner.invoke(cli, ['reorder', 'test_files/MultiPagePDF.pdf', '--out',74 'test_files/out.pdf', '--order', '2,0,1'])75 self.assertEqual(result.exit_code, 0)76 def test_reorder_valid_input_order_indexes_out_of_range(self):77 result = self.runner.invoke(cli, ['reorder', 'test_files/MultiPagePDF.pdf', '--out',78 'test_files/out.pdf', '--order', '2,1,0,4,5,6'])79 self.assertEqual(result.exit_code, 2)80 def test_reorder_valid_input_indexes_duplicated(self):81 result = self.runner.invoke(cli, ['reorder', 'test_files/MultiPagePDF.pdf', '--out',82 'test_files/out.pdf', '--order', '2,1,0,0,1,2'])83 self.assertEqual(result.exit_code, 0)84 def test_reorder_invalid_input_indexes_not_integers(self):85 result = self.runner.invoke(cli, ['reorder', 'test_files/MultiPagePDF.pdf', '--out',86 'test_files/out.pdf', '--order', 'hello,I,am,1'])87 self.assertEqual(result.exit_code, 2)88 def test_reorder_invalid_input_order_empty(self):89 result = self.runner.invoke(cli, ['reorder', 'test_files/MultiPagePDF.pdf', '--out',90 'test_files/out.pdf', '--order', ''])91 def test_reorder_bad_file(self):92 result = self.runner.invoke(cli, ['reorder', 'test_files/test.txt', '--out',93 'test_files/out.pdf', '--order', '1,0,2'])94 self.assertEqual(result.exit_code, 2)95class TestDelete(BasePDFCLITestCase):96 def test_delete_valid_input(self):97 result = self.runner.invoke(cli, ['delete', 'test_files/MultiPagePDF.pdf', '0', '--out', 'test_files/out.pdf'])98 self.assertEqual(result.exit_code, 0)99 with open('test_files/out.pdf', 'rb') as file_reader:100 pdf = PyPDF2.PdfFileReader(file_reader)101 self.assertEqual(pdf.getNumPages(), 2)102 def test_delete_valid_input_two_pages_removed(self):103 result = self.runner.invoke(cli,104 ['delete', 'test_files/MultiPagePDF.pdf', '0,1', '--out', 'test_files/out.pdf'])105 self.assertEqual(result.exit_code, 0)106 with open('test_files/out.pdf', 'rb') as file_reader:107 pdf = PyPDF2.PdfFileReader(file_reader)108 self.assertEqual(pdf.getNumPages(), 1)109 def test_out_of_index_delete(self):110 result = self.runner.invoke(cli,111 ['delete', 'test_files/MultiPagePDF.pdf', '0,5', '--out', 'test_files/out.pdf'])112 self.assertEqual(result.exit_code, 2)113 def test_no_input_delete(self):114 result = self.runner.invoke(cli, ['delete', 'test_files/MultiPagePDF.pdf', '', '--out', 'test_files/out.pdf'])115 self.assertEqual(result.exit_code, 2)116 def test_same_index_specified_twice(self):117 result = self.runner.invoke(cli,118 ['delete', 'test_files/MultiPagePDF.pdf', '0,0,0,1', '--out', 'test_files/out.pdf'])119 self.assertEqual(result.exit_code, 0)120 with open('test_files/out.pdf', 'rb') as file_reader:121 pdf = PyPDF2.PdfFileReader(file_reader)122 self.assertEqual(pdf.getNumPages(), 1)123 def test_delete_invalid_file(self):124 result = self.runner.invoke(cli, ['delete', 'test_files/test.txt', '0,0,0,1', '--out', 'test_files/out.pdf'])125 self.assertEqual(result.exit_code, 2)126 def test_delete_invalid_indexes(self):127 result = self.runner.invoke(cli, ['delete', 'test_files/MultiPagePDF.pdf', 'asdfasd,asdfasf', '--out',128 'test_files/out.pdf'])129 self.assertEqual(result.exit_code, 2)130class TestSplit(BasePDFCLITestCase):131 def test_split_correct_input(self):132 result = self.runner.invoke(cli, ['split', 'test_files/MultiPagePDF.pdf', '1'])133 self.assertEqual(result.exit_code, 0)134 self.assertTrue(os.path.exists('out1.pdf'))135 self.assertTrue(os.path.exists('out2.pdf'))136 with open('out1.pdf', 'rb') as read_fp_one, open('out2.pdf', 'rb') as read_fp_two:137 num_pages_one = PyPDF2.PdfFileReader(read_fp_one).numPages138 self.assertEqual(num_pages_one, 1)139 num_pages_two = PyPDF2.PdfFileReader(read_fp_two).numPages140 self.assertEqual(num_pages_two, 2)141 def test_split_not_integer_split(self):142 result = self.runner.invoke(cli, ['split', 'test_files/MultiPagePDF.pdf', 'asdfasdf'])143 self.assertEqual(result.exit_code, 2)144 def test_split_not_in_range_integer(self):145 result = self.runner.invoke(cli, ['split', 'test_files/MultiPagePDF.pdf', '10'])146 self.assertEqual(result.exit_code, 2)147 def test_split_bad_file(self):148 result = self.runner.invoke(cli, ['split', 'test_files/test.txt', '3'])149 self.assertEqual(result.exit_code, 2)150class TestRotate(BasePDFCLITestCase):151 def test_rotate_clockwise(self):152 result = self.runner.invoke(cli, ['rotate', 'test_files/MultiPagePDF.pdf', 'clockwise'])153 self.assertEqual(result.exit_code, 0)154 self.assertTrue(os.path.exists('out.pdf'))155 def test_rotate_counter_clockwise(self):156 result = self.runner.invoke(cli, ['rotate', 'test_files/MultiPagePDF.pdf', 'counter-clockwise'])157 self.assertEqual(result.exit_code, 0)158 self.assertTrue(os.path.exists('out.pdf'))159 def test_rotate_bad_file(self):160 result = self.runner.invoke(cli, ['rotate', 'test_files.test.txt', 'clockwise'])161 self.assertEqual(result.exit_code, 2)162class TestEncryptDecrypt(BasePDFCLITestCase):163 def test_encrypt(self):164 result = self.runner.invoke(cli, ['encrypt', 'test_files/MultiPagePDF.pdf', '--key', "test_key"])165 self.assertEqual(result.exit_code, 0)166 self.assertTrue(os.path.exists('out.pdf'))167 with self.assertRaises(PyPDF2.utils.PdfReadError), open('out.pdf', 'rb') as reader_fp:168 pdf_reader = PyPDF2.PdfFileReader(reader_fp)169 pdf_reader.getNumPages()170 def test_decrypt(self):171 result = self.runner.invoke(cli, ['encrypt', 'test_files/MultiPagePDF.pdf', '--key', "test_key"])172 self.assertEqual(result.exit_code, 0)173 self.assertTrue(os.path.exists('out.pdf'))174 result = self.runner.invoke(cli, ['decrypt', 'out.pdf', '--out', 'out2.pdf', '--key', "test_key"])175 self.assertEqual(result.exit_code, 0)176 self.assertTrue(os.path.exists('out2.pdf'))177 with open('out2.pdf', 'rb') as reader_fp:178 pdf_reader = PyPDF2.PdfFileReader(reader_fp)179 self.assertEqual(pdf_reader.getNumPages(), 3)180 # Cleanup181 os.remove("out2.pdf")182 def test_decrypt_bad_file(self):183 result = self.runner.invoke(cli, ['encrypt', 'test_files/test.txt', '--key', "test_key"])184 self.assertEqual(result.exit_code, 2)185 def test_encrypt_bad_file(self):186 result = self.runner.invoke(cli, ['decrypt', 'test_files/test.txt', '--key', "test_key"])187 self.assertEqual(result.exit_code, 2)188class TestInfo(BasePDFCLITestCase):189 def test_info(self):190 result = self.runner.invoke(cli, ['info', 'test_files/MultiPagePDF.pdf'])191 self.assertEqual(result.exit_code, 0)192if __name__ == '__main__':...
test_unit.py
Source:test_unit.py
1import pytest2import babelfish3"""4@pytest.fixture5def babelfish(request):6 test_babelfish = app.test_babelfish()7 def teardown():8 pass # need to be freed later9 request.addfinalizer(teardown)10 return test_babelfish11"""12TEST_FILES = [13 "sample.txt",14 "https://raw.githubusercontent.com/unfetter-discover/unfetter-insight/develop/sample.txt",15 "https://www.f-secure.com/documents/996508/1030745/blackenergy_whitepaper.pdf",16 "https://blogs.technet.microsoft.com/srd/2014/05/13/ms14-025-an-update-for-group-policy-preferences/"17 "http://www.akyl.net/securing-bashhistory-file-make-sure-your-linux-system-users-won%E2%80%99t-hide-or-delete-their-bashhistory",18 "https://en.wikipedia.org/wiki/Command-line_interface",19 "https://www.fireeye.com/blog/threat-research/2014/11/operation_doubletap.html",20 "https://en.wikipedia.org/wiki/Code_signing",21 "https://securelist.com/operation-daybreak/75100/"22 "https://www.clearskysec.com/wp-content/uploads/2017/07/Operation_Wilted_Tulip.pdf",23 "https://en.wikipedia.org/wiki/Server_Message_Block",24 "https://www.cylance.com/content/dam/cylance/pdfs/white_papers/RedirectToSMB.pdf"25 "https://osandamalith.com/2017/03/24/places-of-interest-in-stealing-netntlm-hashes/",26 "https://researchcenter.paloaltonetworks.com/2018/02/unit42-sofacy-attacks-multiple-government-entities/",27 "https://www.f-secure.com/documents/996508/1030745/dukes_whitepaper.pdf",28 "http://www.symantec.com/content/en/us/enterprise/media/security_response/whitepapers/the-elderwood-project.pdf",29 "https://www.slideshare.net/MatthewDunwoody1/no-easy-breach-derby-con-2016",30 "https://www.rsaconference.com/writable/presentations/file_upload/hta-f02-detecting-and-responding-to-advanced-threats-within-exchange-environments.pdf",31 "https://www.brighttalk.com/webcast/10703/296317/apt34-new-targeted-attack-in-the-middle-east",32 "https://www2.fireeye.com/WBNR-Know-Your-Enemy-UNC622-Spear-Phishing.html",33 "https://www.fireeye.com/services.html",34 "https://www.trendmicro.de/cloud-content/us/pdfs/security-intelligence/white-papers/wp-finding-holes-operation-emmental.pdf",35 "https://en.wikipedia.org/wiki/Public-key_cryptography",36 "https://researchcenter.paloaltonetworks.com/2016/06/unit42-prince-of-persia-game-over/",37 "https://www.virusbulletin.com/uploads/pdf/conference/vb2014/VB2014-Wardle.pdf",38 "https://www.crowdstrike.com/blog/mo-shells-mo-problems-deep-panda-web-shells/",39 "https://www.us-cert.gov/ncas/alerts/TA17-293A"40]41VERIFY_RESULTS = [42 ("Test_files/sample.txt", "Bypass User Account Control"),43 ("Test_files/Virtual_Private_Keylogging.htm", "Input Capture"),44 ("Test_files/Microsoft_Security_Intelligence_Report.pdf", "Input Capture"),45 ("Test_files/Sowbug_Cyber_espionage.htm", "Input Capture"),46 ("Test_files/rpt_APT37.pdf", "Drive-by Compromise"),47 ("Test_files/The_Shadowserver_Foundation.htm", "Drive-by Compromise"),48 ("Test_files/Targeted_attacks_SouthAsia.pdf", "Drive-by Compromise"),49 ("Test_files/Elderwood_project.htm", "Drive-by Compromise"),50 ("Test_files/How_User_Account_Control_works.htm", "Bypass User Account Control"),51 ("Test_files/blackenergy_whitepaper.pdf", "Bypass User Account Control"),52 ("Test_files/csmanual38.pdf", "Bypass User Account Control"),53 ("Test_files/Public-key_cryptography.htm", "Private Keys"),54 ("Test_files/Prince_of_Persia.htm", "Private Keys"),55 ("Test_files/Cyber_Security_Services_FireEye.htm", "Two-factor Authentication Interception"),56 ("Test_files/JPCERT_CCBlog.htm", "Two-factor Authentication Interception"),57 ("Test_files/Password_cracking.htm", "Brute Force"),58 ("Test_files/APT3_Adversary_Emulation_Plan.pdf", "Brute Force"),59 ("Test_files/Advanced_Persistent_Threat_Activity.htm", "Brute Force"),60 ("Test_files/Command-line_interface.htm", "Command-Line Interface"),61 ("Test_files/mandiant-apt1-report.pdf", "Command-Line Interface"),62 ("Test_files/tacticstechniquesandprocedures.pdf", "Command-Line Interface")63]64"""65@pytest.mark.parametrize("test_input", TEST_FILES)66def test_classify_report(test_input):67 test_list = babelfish.classify_report(test_input)68 assert len(test_list) != 069@pytest.mark.parametrize("test_input", TEST_FILES)70def test_tag_report(test_input):71 plot, text = babelfish.plot_report(test_input)72 x = babelfish.tag_report(text, test_input)73 assert len(x) != 074"""75@pytest.mark.parametrize("files_input,files_output", VERIFY_RESULTS)76def test_classify_report(files_input,files_output):77 test_list = babelfish.classify_report(files_input)...
run_all.py
Source:run_all.py
1import glob, os, sys2test_files = glob.glob('*.py')3test_files.remove('run_all.py')4test_files.remove('allskymap.py')5test_files.remove('fcstmaps.py')6test_files.remove('fcstmaps_axesgrid.py')7test_files.remove('testgdal.py')8test_files.remove('animate.py')9test_files.remove('geos_demo_2.py')10test_files.remove('plotsst.py')11test_files.remove('embedding_map_in_wx.py') # requires wx12test_files.remove('plothighsandlows.py') # requires scipy13test_files.remove('lic_demo.py')14test_files.remove('testwmsimage.py')15try:16 from netCDF4 import Dataset17except ImportError:18 # remove tests requiring netCDF419 sys.stdout.write("Could not import netCDF4, skipping tests that require netCDF4.\n")20 test_files.remove('streamplot_demo.py')21 test_files.remove('plotprecip.py')22 test_files.remove('test_rotpole.py')23 test_files.remove('ccsm_popgrid.py')24 test_files.remove('ploticos.py')25py_path = os.environ.get('PYTHONPATH')26if py_path is None:27 py_path = '.'28else:29 py_path = os.pathsep.join(['.',py_path])30os.environ['PYTHONPATH'] = py_path31for f in test_files:32 sys.stdout.write( "**********************************************\n")33 ff = os.path.join(sys.path[0],f)34 args = [sys.executable,ff]35 sys.stdout.write("Running %s\n" % f)36 status = os.spawnve(os.P_WAIT,sys.executable,args,os.environ)37 if status:...
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!!