Best Python code snippet using unittest-xml-reporting_python
test_gzip.py
Source:test_gzip.py
...30 def tearDown(self):31 test_support.unlink(self.filename)323334 def test_write(self):35 f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)3637 # Try flush and fileno.38 f.flush()39 f.fileno()40 if hasattr(os, 'fsync'):41 os.fsync(f.fileno())42 f.close()4344 # Test multiple close() calls.45 f.close()4647 def test_read(self):48 self.test_write()49 # Try reading.50 f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()51 self.assertEqual(d, data1*50)5253 def test_append(self):54 self.test_write()55 # Append to the previous file56 f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()5758 f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()59 self.assertEqual(d, (data1*50) + (data2*15))6061 def test_many_append(self):62 # Bug #1074261 was triggered when reading a file that contained63 # many, many members. Create such a file and verify that reading it64 # works.65 f = gzip.open(self.filename, 'wb', 9)66 f.write('a')67 f.close()68 for i in range(0,200):69 f = gzip.open(self.filename, "ab", 9) # append70 f.write('a')71 f.close()7273 # Try reading the file74 zgfile = gzip.open(self.filename, "rb")75 contents = ""76 while 1:77 ztxt = zgfile.read(8192)78 contents += ztxt79 if not ztxt: break80 zgfile.close()81 self.assertEquals(contents, 'a'*201)828384 def test_readline(self):85 self.test_write()86 # Try .readline() with varying line lengths8788 f = gzip.GzipFile(self.filename, 'rb')89 line_length = 090 while 1:91 L = f.readline(line_length)92 if L == "" and line_length != 0: break93 self.assert_(len(L) <= line_length)94 line_length = (line_length + 1) % 5095 f.close()9697 def test_readlines(self):98 self.test_write()99 # Try .readlines()100101 f = gzip.GzipFile(self.filename, 'rb')102 L = f.readlines()103 f.close()104105 f = gzip.GzipFile(self.filename, 'rb')106 while 1:107 L = f.readlines(150)108 if L == []: break109 f.close()110111 def test_seek_read(self):112 self.test_write()113 # Try seek, read test114115 f = gzip.GzipFile(self.filename)116 while 1:117 oldpos = f.tell()118 line1 = f.readline()119 if not line1: break120 newpos = f.tell()121 f.seek(oldpos) # negative seek122 if len(line1)>10:123 amount = 10124 else:125 amount = len(line1)126 line2 = f.read(amount)127 self.assertEqual(line1[:amount], line2)128 f.seek(newpos) # positive seek129 f.close()130131 def test_seek_whence(self):132 self.test_write()133 # Try seek(whence=1), read test134135 f = gzip.GzipFile(self.filename)136 f.read(10)137 f.seek(10, whence=1)138 y = f.read(10)139 f.close()140 self.assertEquals(y, data1[20:30])141142 def test_seek_write(self):143 # Try seek, write test144 f = gzip.GzipFile(self.filename, 'w')145 for pos in range(0, 256, 16):146 f.seek(pos)147 f.write('GZ\n')148 f.close()149150 def test_mode(self):151 self.test_write()152 f = gzip.GzipFile(self.filename, 'r')153 self.assertEqual(f.myfileobj.mode, 'rb')154 f.close()155156 def test_1647484(self):157 for mode in ('wb', 'rb'):158 f = gzip.GzipFile(self.filename, mode)159 self.assert_(hasattr(f, "name"))160 self.assertEqual(f.name, self.filename)161 f.close()162163def test_main(verbose=None):164 test_support.run_unittest(TestGzip)165
...
线程锁.py
Source:线程锁.py
1# !/usr/bin/env python2# -*- coding: utf-8 -*-3import threading4import time5"""6å¤çº¿ç¨å¯¹åä¸æ件è¿è¡æä½çæ¶å,æ好è¿å
¥çº¿ç¨éäºæ¥éæ¯çº¿ç¨æ²é,éè¦å
±äº«æ°æ®çæ¶åå çé,è§ä¸ºäºé²æ¢å¤ä¸ªçº¿ç¨åæ¶å¯¹æ°æ®è¿è¡æä½,7èé ææ°æ®çä¸å®å
¨,ä¿è¯åä¸æ¶å»åªè½æä¸ä¸ªçº¿ç¨å¯¹å
¨å±åéè¿è¡æä½,ä½æ¯ä¹å®¹æåºç°æ»é8"""9def test():10 with open("test.txt", "a", encoding="utf-8") as f:11 f.write("test_write" + "\n")12 time.sleep(1)13 mutex.acquire() # åå¾é14 f.close()15 mutex.release() # éæ¾é16if __name__ == '__main__':17 mutex = threading.Lock() # å建é18 for i in range(5):19 t = threading.Thread(target=test)20 t.start()21"""22ä¸å éçç»æ:23test_write24test_write25线ç¨åæ¥è½å¤ä¿è¯å¤ä¸ªçº¿ç¨å®å
¨è®¿é®ç«äºèµæºï¼æç®åçåæ¥æºå¶æ¯å¼å
¥äºæ¥éãäºæ¥é为èµæºå¼å
¥ä¸ä¸ªç¶æï¼éå®/ééå®ã26æ个线ç¨è¦æ´æ¹å
±äº«æ°æ®æ¶ï¼å
å°å
¶éå®ï¼æ¤æ¶èµæºçç¶æ为âéå®âï¼å
¶ä»çº¿ç¨ä¸è½æ´æ¹ï¼ç´å°è¯¥çº¿ç¨éæ¾èµæºï¼å°èµæºçç¶æåæâééå®âï¼27å
¶ä»ç线ç¨æè½å次éå®è¯¥èµæºãäºæ¥éä¿è¯äºæ¯æ¬¡åªæä¸ä¸ªçº¿ç¨è¿è¡åå
¥æä½ï¼ä»èä¿è¯äºå¤çº¿ç¨æ
åµä¸æ°æ®çæ£ç¡®æ§ã28å äºçº¿ç¨éä¹åçç»æ:29test_write30test_write31test_write32test_write33test_write...
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!!