How to use test_writable method in unittest-xml-reporting

Best Python code snippet using unittest-xml-reporting_python

test_utils.py

Source:test_utils.py Github

copy

Full Screen

1import datetime2import hashlib3import os4import pkg_resources5import shutil6import sys7import tempfile8from nose.tools import assert_equals9from onionperf import util10def absolute_data_path(relative_path=""):11 """12 Returns an absolute path for test data given a relative path.13 """14 return pkg_resources.resource_filename("onionperf",15 "tests/data/" +16 relative_path)17DATA_DIR = absolute_data_path()18def test_make_dir():19 """20 Creates a temporary working directory, and then a directory within it.21 The second directory is created using util.make_dir_path.22 Ensures that the path exists, is a directory and is not a symbolic link.23 Removes the temporary working directory only if successful.24 """25 work_dir = tempfile.mkdtemp()26 test_path = os.path.join(work_dir, "test")27 util.make_dir_path(test_path)28 assert(os.path.exists(test_path))29 assert(os.path.isdir(test_path))30 assert(not os.path.islink(test_path))31 shutil.rmtree(work_dir)32def test_find_file_paths():33 """34 Uses util.find_file_paths to find an existing file in the test data directory, given a pattern.35 The function returns the full path to the file.36 """37 pattern = ["abcdef"] 38 paths = util.find_file_paths(DATA_DIR, pattern)39 print(paths)40 assert_equals(paths, [absolute_data_path("dirs/abcdefg.txt")])41 42def test_find_file_paths_with_dash():43 """44 Uses util.find_file_paths to find an existing file in the test data directory, given a45 pattern. Ensures the path returned by the function defaults to stdin if there46 is a dash detected at the end of the given directory.47 """48 pattern = ["abcdef"] 49 paths = util.find_file_paths(DATA_DIR + "/-", pattern)50 assert_equals(paths, ['-'])51 52#def test_find_file_paths_pairs():53# """54# Uses util.find_file_paths_pairs to find existing files in the test data55# directory matching either of two given patterns.56# it returns tuples consisting of a list containing matching file and an empty list.57# The position of the empty lists is dependent on which pattern was matched.58# If a file matches the first pattern, the second item in the tuple will be empty.59# If a file matches the second pattern, the first item in the tuple will be empty.60# """61# first_pattern = ['.*tgen\.log']62# second_pattern = ['.*torctl\.log']63# paths = util.find_file_paths_pairs(DATA_DIR, first_pattern, second_pattern)64# assert_equals(paths, [65# ([], [absolute_data_path("logs/onionperf20190101.torctl.log")]),66# ([], [absolute_data_path("logs/onionperf.torctl.log")]),67# ([absolute_data_path("logs/onionperf.tgen.log")], []),68# ([absolute_data_path("logs/onionperf20190101.tgen.log")], [])69# ])70def test_find_path_with_binpath():71 """72 Creates a temporary named file, uses util.find_path with a filename to find its73 full path and then compares is to that returned by the tempfile function.74 Removes the created named temporary file only if successful.75 """76 temp_file = tempfile.NamedTemporaryFile()77 work_path = util.find_path(temp_file.name, temp_file.name)78 assert_equals(work_path, temp_file.name)79 temp_file.close()80def test_find_path_with_which():81 """82 Creates a temporary named file, and makes it executable.83 Uses util.find_path with a name and search path to find its full84 path, and then compares is to that returned by the tempfile function. Removes85 the created named temporary file only if successful.86 """87 temp_file = tempfile.NamedTemporaryFile()88 os.chmod(temp_file.name, 0o775)89 work_path = util.find_path(None, temp_file.name, tempfile.tempdir)90 assert_equals(work_path, temp_file.name)91 temp_file.close()92def test_is_exe():93 """94 Uses util.is_exe to test if paths point to executable files.95 Checks an executable file path is accepted 96 Checks a non-executable file path is not accepted97 Checks a directory path is not accepted98 """99 assert(util.is_exe(absolute_data_path("bin/script")))100 assert(not util.is_exe(absolute_data_path("bin/script_no_exe")))101 assert(not util.is_exe(absolute_data_path("bin/")))102def test_which():103 """104 Uses util.which with an executable file and a search path in the test data105 directory. Checks the full path of the file is identified.106 """107 test_binary = util.which("script", search_path=absolute_data_path("bin/"))108 assert_equals(test_binary, absolute_data_path("bin/script"))109def test_which_not_executable():110 """111 Uses util.which to test an non-executable file 112 in the test data directory. Checks the non-executable file is not 113 identified as a program to run.114 """115 test_binary = util.which(116 "script_non_exe", search_path=absolute_data_path("bin/"))117 assert_equals(test_binary, None)118def test_which_full_path():119 """120 Uses util.which with the full path of an executable file and a 121 search path. 122 Checks the full path of the file is identified.123 """124 test_binary = util.which(125 absolute_data_path("bin/script"),126 search_path=absolute_data_path("bin/"))127 assert_equals(test_binary, absolute_data_path("bin/script"))128def test_date_to_string():129 """130 Uses util.date_to_string with a datetime object.131 Returns a correctly formatted string.132 """133 date_object = datetime.datetime(2018, 11, 27, 11)134 date_string = util.date_to_string(date_object)135 assert_equals(date_string, "2018-11-27")136def test_date_to_string_is_none():137 """138 Uses util.date_to_string with a None object.139 Returns an empty string.140 """141 date_string = util.date_to_string(None)142 assert_equals(date_string, "")143def test_dates_match():144 """145 Uses util.dates_match with two matching datetime objects.146 """147 first_date = datetime.datetime(2018, 11, 27, 10)148 second_date = datetime.datetime(2018, 11, 27, 11)149 assert(util.do_dates_match(first_date, second_date))150def test_dates_match_false():151 """152 Uses util.dates_match with two non-matching datetime objects.153 """154 first_date = datetime.datetime(2018, 11, 27, 10)155 second_date = datetime.datetime(2016, 11, 27, 11)156 assert_equals(util.do_dates_match(first_date, second_date), False)157def test_find_ip_address_url():158 """159 Uses util.find_ip_address_url with a string containing an IPv4 address.160 The ip address is returned as a string.161 """162 ip_address = util.find_ip_address_url("Your IP address appears to be: 70.70.70.70")163 assert_equals(ip_address, "70.70.70.70")164def test_find_ip_address_url_none():165 """166 Uses util.find_ip_address_url with a string containing no IPv4 address.167 This should return None.168 """169 ip_address = util.find_ip_address_url("Your IP address appears to be")170 assert_equals(ip_address, None)171def test_get_random_free_port():172 """173 Uses util.get_random_free_port to get a port number.174 Asserts the port exists, and it is a high-numbered port175 between 10000 and 60000.176 """ 177 port = util.get_random_free_port()178 assert(port is not None)179 assert(port < 60000)180 assert(port >= 10000)181def test_data_source_stdin():182 """183 Creates a new util.DataSource object with stdin input. When calling184 util.DataSource.open(), this should set stdin as the DataSource.source185 for this object.186 """187 test_data_source = util.DataSource("-")188 test_data_source.open()189 assert_equals(test_data_source.source, sys.stdin)190def test_data_source_file():191 """192 Creates a new util.DataSource object with an uncompressed input file. When calling193 util.DataSource.open(), this should set the file handle as the DataSource.source194 for this object. 195 DataSouce.source is verified against the contents of the input file.196 """197 test_data_source = util.DataSource(absolute_data_path("simplefile"))198 test_data_source.open()199 data_source_file_handle = test_data_source.source200 data_source_contents = data_source_file_handle.read()201 assert_equals(data_source_contents, "onionperf")202def test_data_source_compressed_file():203 """204 Creates a new util.DataSource object with a compressed input file. When205 calling util.DataSource.open(), this should set the output of an xzprocess (an206 uncompressed file handle) as the DataSource.source for this object, and set207 DataSource.compress to True. 208 Verifies DataSource.compress is set to True. 209 DataSouce.source is verified against the contents of the input file.210 """211 test_data_source = util.DataSource(absolute_data_path("simplefile.xz"))212 test_data_source.open()213 data_source_file_handle = test_data_source.source214 data_source_contents = data_source_file_handle.read()215 assert_equals(data_source_contents, "onionperf")216 assert(test_data_source.compress)217def test_file_writable():218 """219 Creates a new util.FileWritable object using a temporary filename.220 Writes a string to it using util.FileWritable.write(). 221 The checksum of this file is compared to a good known checksum. 222 The temporary file is only removed if the test is successful.223 """224 temp_file = tempfile.NamedTemporaryFile()225 test_writable = util.FileWritable(temp_file.name)226 test_writable.write("onionperf")227 test_writable.close()228 expected_checksum = "5001ed4ab25b52543946fa63da829d4eeab1bd254c89ffdad0877186e074b385"229 with open(temp_file.name, 'rb') as f:230 file_bytes = f.read()231 file_checksum = hashlib.sha256(file_bytes).hexdigest()232 assert_equals(file_checksum, expected_checksum)233 temp_file.close()234def test_file_writable_compressed():235 """236 Creates a new util.FileWritable object using a temporary filename and237 compression. Writes a string to it using util.FileWritable.write(). 238 The checksum of this file is compared to a good known checksum. 239 The temporary file is only removed if the test is successful.240 """241 temp_file = tempfile.NamedTemporaryFile(suffix=".xz")242 test_writable = util.FileWritable(temp_file.name, True)243 test_writable.write("onionperf")244 test_writable.close()245 expected_checksum = "3556b3bee6bb56d0a42676cbbf5784ebe4151fe65b0797f42260f93212e2df11"246 with open(temp_file.name, 'rb') as f:247 file_bytes = f.read()248 file_checksum = hashlib.sha256(file_bytes).hexdigest()249 assert_equals(file_checksum, expected_checksum)250 temp_file.close()251def test_file_writable_with_stout():252 """253 Creates a new util.FileWritable object using stdout.254 Checks the util.FileWritable.file attribute is set to stdout.255 """256 test_writable = util.FileWritable("-")257 assert_equals(test_writable.file, sys.stdout)258def test_file_writable_rotate_file():259 """260 Creates a temporary working directory.261 Creates a new util.FileWritable object in the working directory.262 Rotates file using util.FileWritable.rotate_file with a fixed date and time.263 Checks path log_archive has been created in the working directory.264 Checks path log_archive is a directory.265 Checks file with the appropiate name has been rotated in the log_archive directory.266 Removes working directory only if successful.267 """268 work_dir = tempfile.mkdtemp()269 test_writable = util.FileWritable(os.path.join(work_dir, "logfile"))270 test_writable.write("onionperf")271 test_writable.rotate_file(datetime.datetime(2018, 11, 27, 0, 0, 0))272 created_dir = os.path.join(work_dir, "log_archive")273 rotated_file = os.path.join(created_dir, "logfile_2018-11-27_00:00:00.gz")274 assert(os.path.exists(created_dir))275 assert(os.path.isdir(created_dir))276 assert(os.path.exists(rotated_file))...

Full Screen

Full Screen

test_sinks.py

Source:test_sinks.py Github

copy

Full Screen

...27 self.fileobj.push(self.filter)28 self.bio = self.fileobj.c_bio29 def test_readable(self):30 self.assertTrue(self.fileobj.readable())31 def test_writable(self):32 self.assertFalse(self.fileobj.writable())33class TestFileWrite(TestChainWrite):34 @classmethod35 def setUpClass(cls):36 fd, cls.filename = tempfile.mkstemp()37 os.close(fd)38 @classmethod39 def tearDownClass(cls):40 os.remove(cls.filename)41 def setUp(self):42 self.fileobj = io.BIOFile(self.filename, 'w')43 self.filter = api.BIO_new(api.BIO_f_null())44 self.sink = self.fileobj.c_bio45 self.fileobj.push(self.filter)46 self.bio = self.fileobj.c_bio47 def test_readable(self):48 self.assertFalse(self.fileobj.readable())49 def test_writable(self):50 self.assertTrue(self.fileobj.writable())51class TestFileBio(unittest.TestCase):52 @mock.patch('tls.err.logger')53 def test_invalid_filename(self, _):54 self.assertRaises(IOError, io.BIOFile, '_not_a_file', 'r')55 def test_invalid_mode(self):56 self.assertRaises(ValueError, io.BIOFile, '', 'x')57class TestMemBufferWrite(TestChainWrite):58 def setUp(self):59 self.fileobj = io.BIOMemBuffer()60 self.filter = api.BIO_new(api.BIO_f_null())61 self.sink = self.fileobj.c_bio62 self.fileobj.push(self.filter)63 self.bio = self.fileobj.c_bio64class TestMemBufferRead(TestChainRead):65 def setUp(self):66 self.fileobj = io.BIOMemBuffer(self.DATA)67 self.filter = api.BIO_new(api.BIO_f_null())68 self.sink = self.fileobj.c_bio69 self.fileobj.push(self.filter)70 self.bio = self.fileobj.c_bio71 def test_writable(self):72 self.assertFalse(self.fileobj.writable())73class TestNullBio(ChainTest, unittest.TestCase):74 def setUp(self):75 self.fileobj = io.BIONull()76 self.filter = api.BIO_new(api.BIO_f_null())77 self.sink = self.fileobj.c_bio78 self.fileobj.push(self.filter)79 self.bio = self.fileobj.c_bio80 def tearDown(self):81 if self.fileobj.c_bio is not api.NULL:...

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