How to use test_write method in unittest-xml-reporting

Best Python code snippet using unittest-xml-reporting_python

test_gzip.py

Source:test_gzip.py Github

copy

Full Screen

...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 ...

Full Screen

Full Screen

线程锁.py

Source:线程锁.py Github

copy

Full Screen

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...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run unittest-xml-reporting automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful