How to use str_filesystem method in avocado

Best Python code snippet using avocado_python

test_test.py

Source:test_test.py Github

copy

Full Screen

1import os2import shutil3import tempfile4import unittest5try:6 from unittest import mock7except ImportError:8 import mock9from avocado.core import test, exceptions10from avocado.utils import astring, script11PASS_SCRIPT_CONTENTS = """#!/bin/sh12true13"""14FAIL_SCRIPT_CONTENTS = """#!/bin/sh15false16"""17class TestClassTestUnit(unittest.TestCase):18 class DummyTest(test.Test):19 def test(self):20 pass21 def setUp(self):22 self.tmpdir = tempfile.mkdtemp(prefix="avocado_" + __name__)23 def _get_fake_filename_test(self, name):24 class FakeFilename(test.Test):25 @property26 def filename(self):27 return name28 def test(self):29 pass30 tst_id = test.TestID("test", name=name)31 return FakeFilename("test", tst_id, base_logdir=self.tmpdir)32 def tearDown(self):33 shutil.rmtree(self.tmpdir)34 def test_ugly_name(self):35 def run(name, path_name):36 """ Initialize test and check the dirs were created """37 tst = self.DummyTest("test", test.TestID(1, name),38 base_logdir=self.tmpdir)39 self.assertEqual(os.path.basename(tst.logdir), path_name)40 self.assertTrue(os.path.exists(tst.logdir))41 self.assertEqual(os.path.dirname(os.path.dirname(tst.logdir)),42 self.tmpdir)43 run("/absolute/path", "1-_absolute_path")44 run("./relative/path", "1-._relative_path")45 run("../../multi_level/relative/path",46 "1-.._.._multi_level_relative_path")47 # Greek word 'kosme'48 run("\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5",49 "1-\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5")50 # Particularly problematic noncharacters in 16-bit applications51 name = ("\xb7\x95\xef\xb7\x96\xef\xb7\x97\xef\xb7\x98\xef\xb7\x99"52 "\xef\xb7\x9a\xef\xb7\x9b\xef\xb7\x9c\xef\xb7\x9d\xef\xb7"53 "\x9e\xef\xb7\x9f\xef\xb7\xa0\xef\xb7\xa1\xef\xb7\xa2\xef"54 "\xb7\xa3\xef\xb7\xa4\xef\xb7\xa5\xef\xb7\xa6\xef\xb7\xa7"55 "\xef\xb7\xa8\xef\xb7\xa9\xef\xb7\xaa\xef\xb7\xab\xef\xb7"56 "\xac\xef\xb7\xad\xef\xb7\xae\xef\xb7\xaf")57 run(name, "1-" + name)58 def test_long_name(self):59 def check(uid, name, variant, exp_logdir):60 tst = self.DummyTest("test", test.TestID(uid, name, variant),61 base_logdir=self.tmpdir)62 self.assertEqual(os.path.basename(tst.logdir), exp_logdir)63 return tst64 # Everything fits65 check(1, "a" * 253, None, "1-" + ("a" * 253))66 check(2, "a" * 251, {"variant_id": 1}, "2-" + ("a" * 251) + "_1")67 check(99, "a" * 249, {"variant_id": 88}, "99-" + ("a" * 249) + "_88")68 # Shrink name69 check(3, "a" * 252, {"variant_id": 1}, "3-" + ('a' * 251) + "_1")70 # Shrink variant71 check("a" * 253, "whatever", {"variant_id": 99}, "a" * 253 + "_9")72 check("a" * 254, "whatever", {"variant_id": 99}, "a" * 254 + "_")73 # No variant74 tst = check("a" * 255, "whatever", {"variant_id": "whatever-else"},75 "a" * 255)76 # Impossible to store (uid does not fit77 self.assertRaises(RuntimeError, check, "a" * 256, "whatever",78 {"variant_id": "else"}, None)79 self.assertEqual(os.path.basename(tst.workdir),80 os.path.basename(tst.logdir))81 def test_data_dir(self):82 """83 Checks `get_data()` won't report fs-unfriendly data dir name84 """85 max_length_name = os.path.join(self.tmpdir, "a" * 250)86 tst = self._get_fake_filename_test(max_length_name)87 self.assertEqual(os.path.join(self.tmpdir, max_length_name + ".data"),88 tst.get_data('', 'file', False))89 def test_no_data_dir(self):90 """91 Tests that with a filename too long, no datadir is possible92 """93 above_limit_name = os.path.join(self.tmpdir, "a" * 251)94 tst = self._get_fake_filename_test(above_limit_name)95 self.assertFalse(tst.get_data('', 'file', False))96 tst._record_reference('stdout', 'stdout.expected')97 tst._record_reference('stderr', 'stderr.expected')98 tst._record_reference('output', 'output.expected')99 def test_all_dirs_exists_no_hang(self):100 with mock.patch('os.path.exists', return_value=True):101 self.assertRaises(exceptions.TestSetupFail, self.DummyTest, "test",102 test.TestID(1, "name"), base_logdir=self.tmpdir)103 def test_try_override_test_variable(self):104 dummy_test = self.DummyTest(base_logdir=self.tmpdir)105 self.assertRaises(AttributeError, setattr, dummy_test, "name", "whatever")106 self.assertRaises(AttributeError, setattr, dummy_test, "status", "whatever")107 def test_check_reference_success(self):108 '''109 Tests that a check is made, and is successful110 '''111 class GetDataTest(test.Test):112 def test(self):113 pass114 def get_data(self, filename, source=None, must_exist=True):115 # return the filename (path, really) unchanged116 return filename117 tst = GetDataTest("test", test.TestID(1, "test"),118 base_logdir=self.tmpdir)119 content = 'expected content\n'120 content_path = os.path.join(tst.logdir, 'content')121 with open(content_path, 'w') as produced:122 produced.write(content)123 self.assertTrue(tst._check_reference(content_path,124 content_path,125 'content.diff',126 'content_diff',127 'Content'))128 def test_check_reference_does_not_exist(self):129 '''130 Tests that a check is not made for a file that does not exist131 '''132 tst = self.DummyTest("test", test.TestID(1, "test"),133 base_logdir=self.tmpdir)134 self.assertFalse(tst._check_reference('does_not_exist',135 'stdout.expected',136 'stdout.diff',137 'stdout_diff',138 'Stdout'))139class TestClassTest(unittest.TestCase):140 def setUp(self):141 class AvocadoPass(test.Test):142 def test(self):143 variable = True144 self.assertTrue(variable)145 self.whiteboard = 'foo'146 self.base_logdir = tempfile.mkdtemp(prefix='avocado_' + __name__)147 self.tst_instance_pass = AvocadoPass(base_logdir=self.base_logdir)148 self.tst_instance_pass.run_avocado()149 def test_class_attributes_name(self):150 self.assertEqual(self.tst_instance_pass.name, '0-AvocadoPass')151 def test_class_attributes_status(self):152 self.assertEqual(self.tst_instance_pass.status, 'PASS')153 def test_class_attributes_time_elapsed(self):154 self.assertIsInstance(self.tst_instance_pass.time_elapsed, float)155 def test_whiteboard_save(self):156 whiteboard_file = os.path.join(157 self.tst_instance_pass.logdir, 'whiteboard')158 self.assertTrue(os.path.isfile(whiteboard_file))159 with open(whiteboard_file, 'r') as whiteboard_file_obj:160 whiteboard_contents = whiteboard_file_obj.read().strip()161 self.assertTrue(whiteboard_contents, 'foo')162 def test_running_test_twice_with_the_same_uid_failure(self):163 class AvocadoPass(test.Test):164 def test(self):165 pass166 self.assertRaises(exceptions.TestSetupFail, AvocadoPass,167 base_logdir=self.base_logdir)168 def tearDown(self):169 shutil.rmtree(self.base_logdir)170class SimpleTestClassTest(unittest.TestCase):171 def setUp(self):172 self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)173 self.script = None174 def test_simple_test_pass_status(self):175 self.script = script.TemporaryScript(176 'avocado_pass.sh',177 PASS_SCRIPT_CONTENTS,178 'avocado_simpletest_unittest')179 self.script.save()180 tst_instance = test.SimpleTest(181 name=test.TestID(1, self.script.path),182 base_logdir=self.tmpdir)183 tst_instance.run_avocado()184 self.assertEqual(tst_instance.status, 'PASS')185 def test_simple_test_fail_status(self):186 self.script = script.TemporaryScript(187 'avocado_fail.sh',188 FAIL_SCRIPT_CONTENTS,189 'avocado_simpletest_unittest')190 self.script.save()191 tst_instance = test.SimpleTest(192 name=test.TestID(1, self.script.path),193 base_logdir=self.tmpdir)194 tst_instance.run_avocado()195 self.assertEqual(tst_instance.status, 'FAIL')196 def tearDown(self):197 if self.script is not None:198 self.script.remove()199 shutil.rmtree(self.tmpdir)200class MockingTest(unittest.TestCase):201 def setUp(self):202 self.tests = []203 def test_init(self):204 # No params205 self.tests.append(test.MockingTest())206 # Positional207 self.tests.append(test.MockingTest("test", test.TestID(1, "my_name"),208 {}, None, "1",209 None, None, "extra_param1",210 "extra_param2"))211 self.assertEqual(self.tests[-1].name, "1-my_name")212 # Kwargs213 self.tests.append(test.MockingTest(methodName="test",214 name=test.TestID(1, "my_name2"),215 params={}, base_logdir=None,216 tag="a", job=None, runner_queue=None,217 extra1="extra_param1",218 extra2="extra_param2"))219 self.assertEqual(self.tests[-1].name, "1-my_name2")220 # both (theoretically impossible in python, but valid for nasty tests)221 # keyword args are used as they explicitly represent what they mean222 self.tests.append(test.MockingTest("not used", "who cares", {}, None, "0",223 None, None, "extra_param1",224 "extra_param2",225 methodName="test",226 name=test.TestID(1, "my_name3"),227 params={}, base_logdir=None,228 tag="3", job=None, runner_queue=None,229 extra1="extra_param3",230 extra2="extra_param4"))231 self.assertEqual(self.tests[-1].name, "1-my_name3")232 # combination233 self.tests.append(test.MockingTest("test", test.TestID(1, "my_name4"),234 tag="321",235 other_param="Whatever"))236 self.assertEqual(self.tests[-1].name, "1-my_name4")237 # ugly combination (positional argument overrides kwargs, this only238 # happens when the substituted class reorders the positional arguments.239 # We try to first match keyword args and then fall-back to positional240 # ones.241 name = "positional_method_name_becomes_test_name"242 tag = "positional_base_logdir_becomes_tag"243 self.tests.append(test.MockingTest(test.TestID(1, name), None, None, tag,244 methodName="test",245 other_param="Whatever"))246 self.assertEqual(self.tests[-1].name, "1-" + name)247 def tearDown(self):248 for tst in self.tests:249 try:250 shutil.rmtree(os.path.dirname(os.path.dirname(tst.logdir)))251 except Exception:252 pass253class TestID(unittest.TestCase):254 def test_uid_name(self):255 uid = 1256 name = 'file.py:klass.test_method'257 test_id = test.TestID(uid, name)258 self.assertEqual(test_id.uid, 1)259 self.assertEqual(test_id.str_uid, '1')260 self.assertEqual(test_id.str_filesystem,261 astring.string_to_safe_path('%s-%s' % (uid, name)))262 self.assertIs(test_id.variant, None)263 self.assertIs(test_id.str_variant, '')264 def test_uid_name_no_digits(self):265 uid = 1266 name = 'file.py:klass.test_method'267 test_id = test.TestID(uid, name, no_digits=2)268 self.assertEqual(test_id.uid, 1)269 self.assertEqual(test_id.str_uid, '01')270 self.assertEqual(test_id.str_filesystem,271 astring.string_to_safe_path('%s-%s' % ('01', name)))272 self.assertIs(test_id.variant, None)273 self.assertIs(test_id.str_variant, '')274 def test_uid_name_large_digits(self):275 """276 Tests that when the filesystem can only cope with the size of277 the Test ID, that's the only thing that will be kept.278 """279 uid = 1280 name = 'test'281 test_id = test.TestID(uid, name, no_digits=255)282 self.assertEqual(test_id.uid, 1)283 self.assertEqual(test_id.str_uid, '%0255i' % uid)284 self.assertEqual(test_id.str_filesystem, '%0255i' % uid)285 self.assertIs(test_id.variant, None)286 self.assertIs(test_id.str_variant, '')287 def test_uid_name_uid_too_large_digits(self):288 """289 Tests that when the filesystem can not cope with the size of290 the Test ID, not even the test uid, an exception will be291 raised.292 """293 test_id = test.TestID(1, 'test', no_digits=256)294 self.assertRaises(RuntimeError, lambda: test_id.str_filesystem)295 def test_uid_large_name(self):296 """297 Tests that when the filesystem can not cope with the size of298 the Test ID, the name will be shortened.299 """300 uid = 1301 name = 'test_' * 51 # 255 characters302 test_id = test.TestID(uid, name)303 self.assertEqual(test_id.uid, 1)304 # only 253 can fit for the test name305 self.assertEqual(test_id.str_filesystem, '%s-%s' % (uid, name[:253]))306 self.assertIs(test_id.variant, None)307 self.assertIs(test_id.str_variant, "")308 def test_uid_name_large_variant(self):309 """310 Tests that when the filesystem can not cope with the size of311 the Test ID, and a variant name is present, the name will be312 removed.313 """314 uid = 1315 name = 'test'316 variant_id = 'fast_' * 51 # 255 characters317 variant = {'variant_id': variant_id}318 test_id = test.TestID(uid, name, variant=variant)319 self.assertEqual(test_id.uid, 1)320 self.assertEqual(test_id.str_filesystem, '%s_%s' % (uid, variant_id[:253]))321 self.assertIs(test_id.variant, variant_id)322 self.assertEqual(test_id.str_variant, ";%s" % variant_id)323if __name__ == '__main__':...

Full Screen

Full Screen

test_test_id.py

Source:test_test_id.py Github

copy

Full Screen

1import unittest2from avocado.core.test_id import TestID3from avocado.utils import astring4class Test(unittest.TestCase):5 def test_uid_name(self):6 uid = 17 name = 'file.py:klass.test_method'8 test_id = TestID(uid, name)9 self.assertEqual(test_id.uid, 1)10 self.assertEqual(test_id.str_uid, '1')11 self.assertEqual(test_id.str_filesystem,12 astring.string_to_safe_path('%s-%s' % (uid, name)))13 self.assertIs(test_id.variant, None)14 self.assertIs(test_id.str_variant, '')15 def test_uid_name_no_digits(self):16 uid = 117 name = 'file.py:klass.test_method'18 test_id = TestID(uid, name, no_digits=2)19 self.assertEqual(test_id.uid, 1)20 self.assertEqual(test_id.str_uid, '01')21 self.assertEqual(test_id.str_filesystem,22 astring.string_to_safe_path('%s-%s' % ('01', name)))23 self.assertIs(test_id.variant, None)24 self.assertIs(test_id.str_variant, '')25 def test_uid_name_large_digits(self):26 """27 Tests that when the filesystem can only cope with the size of28 the Test ID, that's the only thing that will be kept.29 """30 uid = 131 name = 'test'32 test_id = TestID(uid, name, no_digits=255)33 self.assertEqual(test_id.uid, 1)34 self.assertEqual(test_id.str_uid, '%0255i' % uid)35 self.assertEqual(test_id.str_filesystem, '%0255i' % uid)36 self.assertIs(test_id.variant, None)37 self.assertIs(test_id.str_variant, '')38 def test_uid_name_uid_too_large_digits(self):39 """40 Tests that when the filesystem can not cope with the size of41 the Test ID, not even the test uid, an exception will be42 raised.43 """44 test_id = TestID(1, 'test', no_digits=256)45 self.assertRaises(RuntimeError, lambda: test_id.str_filesystem)46 def test_uid_large_name(self):47 """48 Tests that when the filesystem can not cope with the size of49 the Test ID, the name will be shortened.50 """51 uid = 152 name = 'test_' * 51 # 255 characters53 test_id = TestID(uid, name)54 self.assertEqual(test_id.uid, 1)55 # only 253 can fit for the test name56 self.assertEqual(test_id.str_filesystem, '%s-%s' % (uid, name[:253]))57 self.assertIs(test_id.variant, None)58 self.assertIs(test_id.str_variant, "")59 def test_uid_name_large_variant(self):60 """61 Tests that when the filesystem can not cope with the size of62 the Test ID, and a variant name is present, the name will be63 removed.64 """65 uid = 166 name = 'test'67 variant_id = 'fast_' * 51 # 255 characters68 variant = {'variant_id': variant_id}69 test_id = TestID(uid, name, variant=variant)70 self.assertEqual(test_id.uid, 1)71 self.assertEqual(test_id.str_filesystem, '%s_%s' % (uid, variant_id[:253]))72 self.assertIs(test_id.variant, variant_id)73 self.assertEqual(test_id.str_variant, ";%s" % variant_id)74if __name__ == '__main__':...

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