Best Python code snippet using tempest_python
test_discovery.py
Source:test_discovery.py
...58 loader._get_module_from_name = lambda path: path + ' module'59 orig_load_tests = loader.loadTestsFromModule60 def loadTestsFromModule(module, pattern=None):61 # This is where load_tests is called.62 base = orig_load_tests(module, pattern=pattern)63 return base + [module + ' tests']64 loader.loadTestsFromModule = loadTestsFromModule65 loader.suiteClass = lambda thing: thing66 top_level = os.path.abspath('/foo')67 loader._top_level_dir = top_level68 suite = list(loader._find_tests(top_level, 'test*.py'))69 # The test suites found should be sorted alphabetically for reliable70 # execution order.71 expected = [[name + ' module tests'] for name in72 ('test1', 'test2', 'test_dir')]73 expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in74 ('test3', 'test4')])75 self.assertEqual(suite, expected)76 def test_find_tests_socket(self):77 # A socket is neither a directory nor a regular file.78 # https://bugs.python.org/issue2532079 loader = unittest.TestLoader()80 original_listdir = os.listdir81 def restore_listdir():82 os.listdir = original_listdir83 original_isfile = os.path.isfile84 def restore_isfile():85 os.path.isfile = original_isfile86 original_isdir = os.path.isdir87 def restore_isdir():88 os.path.isdir = original_isdir89 path_lists = [['socket']]90 os.listdir = lambda path: path_lists.pop(0)91 self.addCleanup(restore_listdir)92 os.path.isdir = lambda path: False93 self.addCleanup(restore_isdir)94 os.path.isfile = lambda path: False95 self.addCleanup(restore_isfile)96 loader._get_module_from_name = lambda path: path + ' module'97 orig_load_tests = loader.loadTestsFromModule98 def loadTestsFromModule(module, pattern=None):99 # This is where load_tests is called.100 base = orig_load_tests(module, pattern=pattern)101 return base + [module + ' tests']102 loader.loadTestsFromModule = loadTestsFromModule103 loader.suiteClass = lambda thing: thing104 top_level = os.path.abspath('/foo')105 loader._top_level_dir = top_level106 suite = list(loader._find_tests(top_level, 'test*.py'))107 self.assertEqual(suite, [])108 def test_find_tests_with_package(self):109 loader = unittest.TestLoader()110 original_listdir = os.listdir111 def restore_listdir():112 os.listdir = original_listdir113 original_isfile = os.path.isfile114 def restore_isfile():115 os.path.isfile = original_isfile116 original_isdir = os.path.isdir117 def restore_isdir():118 os.path.isdir = original_isdir119 directories = ['a_directory', 'test_directory', 'test_directory2']120 path_lists = [directories, [], [], []]121 os.listdir = lambda path: path_lists.pop(0)122 self.addCleanup(restore_listdir)123 os.path.isdir = lambda path: True124 self.addCleanup(restore_isdir)125 os.path.isfile = lambda path: os.path.basename(path) not in directories126 self.addCleanup(restore_isfile)127 class Module(object):128 paths = []129 load_tests_args = []130 def __init__(self, path):131 self.path = path132 self.paths.append(path)133 if os.path.basename(path) == 'test_directory':134 def load_tests(loader, tests, pattern):135 self.load_tests_args.append((loader, tests, pattern))136 return [self.path + ' load_tests']137 self.load_tests = load_tests138 def __eq__(self, other):139 return self.path == other.path140 loader._get_module_from_name = lambda name: Module(name)141 orig_load_tests = loader.loadTestsFromModule142 def loadTestsFromModule(module, pattern=None):143 # This is where load_tests is called.144 base = orig_load_tests(module, pattern=pattern)145 return base + [module.path + ' module tests']146 loader.loadTestsFromModule = loadTestsFromModule147 loader.suiteClass = lambda thing: thing148 loader._top_level_dir = '/foo'149 # this time no '.py' on the pattern so that it can match150 # a test package151 suite = list(loader._find_tests('/foo', 'test*'))152 # We should have loaded tests from the a_directory and test_directory2153 # directly and via load_tests for the test_directory package, which154 # still calls the baseline module loader.155 self.assertEqual(suite,156 [['a_directory module tests'],157 ['test_directory load_tests',158 'test_directory module tests'],159 ['test_directory2 module tests']])160 # The test module paths should be sorted for reliable execution order161 self.assertEqual(Module.paths,162 ['a_directory', 'test_directory', 'test_directory2'])163 # load_tests should have been called once with loader, tests and pattern164 # (but there are no tests in our stub module itself, so that is [] at165 # the time of call).166 self.assertEqual(Module.load_tests_args,167 [(loader, [], 'test*')])168 def test_find_tests_default_calls_package_load_tests(self):169 loader = unittest.TestLoader()170 original_listdir = os.listdir171 def restore_listdir():172 os.listdir = original_listdir173 original_isfile = os.path.isfile174 def restore_isfile():175 os.path.isfile = original_isfile176 original_isdir = os.path.isdir177 def restore_isdir():178 os.path.isdir = original_isdir179 directories = ['a_directory', 'test_directory', 'test_directory2']180 path_lists = [directories, [], [], []]181 os.listdir = lambda path: path_lists.pop(0)182 self.addCleanup(restore_listdir)183 os.path.isdir = lambda path: True184 self.addCleanup(restore_isdir)185 os.path.isfile = lambda path: os.path.basename(path) not in directories186 self.addCleanup(restore_isfile)187 class Module(object):188 paths = []189 load_tests_args = []190 def __init__(self, path):191 self.path = path192 self.paths.append(path)193 if os.path.basename(path) == 'test_directory':194 def load_tests(loader, tests, pattern):195 self.load_tests_args.append((loader, tests, pattern))196 return [self.path + ' load_tests']197 self.load_tests = load_tests198 def __eq__(self, other):199 return self.path == other.path200 loader._get_module_from_name = lambda name: Module(name)201 orig_load_tests = loader.loadTestsFromModule202 def loadTestsFromModule(module, pattern=None):203 # This is where load_tests is called.204 base = orig_load_tests(module, pattern=pattern)205 return base + [module.path + ' module tests']206 loader.loadTestsFromModule = loadTestsFromModule207 loader.suiteClass = lambda thing: thing208 loader._top_level_dir = '/foo'209 # this time no '.py' on the pattern so that it can match210 # a test package211 suite = list(loader._find_tests('/foo', 'test*.py'))212 # We should have loaded tests from the a_directory and test_directory2213 # directly and via load_tests for the test_directory package, which214 # still calls the baseline module loader.215 self.assertEqual(suite,216 [['a_directory module tests'],217 ['test_directory load_tests',218 'test_directory module tests'],219 ['test_directory2 module tests']])220 # The test module paths should be sorted for reliable execution order221 self.assertEqual(Module.paths,222 ['a_directory', 'test_directory', 'test_directory2'])223 # load_tests should have been called once with loader, tests and pattern224 self.assertEqual(Module.load_tests_args,225 [(loader, [], 'test*.py')])226 def test_find_tests_customize_via_package_pattern(self):227 # This test uses the example 'do-nothing' load_tests from228 # https://docs.python.org/3/library/unittest.html#load-tests-protocol229 # to make sure that that actually works.230 # Housekeeping231 original_listdir = os.listdir232 def restore_listdir():233 os.listdir = original_listdir234 self.addCleanup(restore_listdir)235 original_isfile = os.path.isfile236 def restore_isfile():237 os.path.isfile = original_isfile238 self.addCleanup(restore_isfile)239 original_isdir = os.path.isdir240 def restore_isdir():241 os.path.isdir = original_isdir242 self.addCleanup(restore_isdir)243 self.addCleanup(sys.path.remove, abspath('/foo'))244 # Test data: we expect the following:245 # a listdir to find our package, and isfile and isdir checks on it.246 # a module-from-name call to turn that into a module247 # followed by load_tests.248 # then our load_tests will call discover() which is messy249 # but that finally chains into find_tests again for the child dir -250 # which is why we don't have an infinite loop.251 # We expect to see:252 # the module load tests for both package and plain module called,253 # and the plain module result nested by the package module load_tests254 # indicating that it was processed and could have been mutated.255 vfs = {abspath('/foo'): ['my_package'],256 abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}257 def list_dir(path):258 return list(vfs[path])259 os.listdir = list_dir260 os.path.isdir = lambda path: not path.endswith('.py')261 os.path.isfile = lambda path: path.endswith('.py')262 class Module(object):263 paths = []264 load_tests_args = []265 def __init__(self, path):266 self.path = path267 self.paths.append(path)268 if path.endswith('test_module'):269 def load_tests(loader, tests, pattern):270 self.load_tests_args.append((loader, tests, pattern))271 return [self.path + ' load_tests']272 else:273 def load_tests(loader, tests, pattern):274 self.load_tests_args.append((loader, tests, pattern))275 # top level directory cached on loader instance276 __file__ = '/foo/my_package/__init__.py'277 this_dir = os.path.dirname(__file__)278 pkg_tests = loader.discover(279 start_dir=this_dir, pattern=pattern)280 return [self.path + ' load_tests', tests281 ] + pkg_tests282 self.load_tests = load_tests283 def __eq__(self, other):284 return self.path == other.path285 loader = unittest.TestLoader()286 loader._get_module_from_name = lambda name: Module(name)287 loader.suiteClass = lambda thing: thing288 loader._top_level_dir = abspath('/foo')289 # this time no '.py' on the pattern so that it can match290 # a test package291 suite = list(loader._find_tests(abspath('/foo'), 'test*.py'))292 # We should have loaded tests from both my_package and293 # my_package.test_module, and also run the load_tests hook in both.294 # (normally this would be nested TestSuites.)295 self.assertEqual(suite,296 [['my_package load_tests', [],297 ['my_package.test_module load_tests']]])298 # Parents before children.299 self.assertEqual(Module.paths,300 ['my_package', 'my_package.test_module'])301 # load_tests should have been called twice with loader, tests and pattern302 self.assertEqual(Module.load_tests_args,303 [(loader, [], 'test*.py'),304 (loader, [], 'test*.py')])305 def test_discover(self):306 loader = unittest.TestLoader()307 original_isfile = os.path.isfile308 original_isdir = os.path.isdir309 def restore_isfile():310 os.path.isfile = original_isfile311 os.path.isfile = lambda path: False312 self.addCleanup(restore_isfile)313 orig_sys_path = sys.path[:]314 def restore_path():315 sys.path[:] = orig_sys_path316 self.addCleanup(restore_path)317 full_path = os.path.abspath(os.path.normpath('/foo'))318 with self.assertRaises(ImportError):319 loader.discover('/foo/bar', top_level_dir='/foo')320 self.assertEqual(loader._top_level_dir, full_path)321 self.assertIn(full_path, sys.path)322 os.path.isfile = lambda path: True323 os.path.isdir = lambda path: True324 def restore_isdir():325 os.path.isdir = original_isdir326 self.addCleanup(restore_isdir)327 _find_tests_args = []328 def _find_tests(start_dir, pattern, namespace=None):329 _find_tests_args.append((start_dir, pattern))330 return ['tests']331 loader._find_tests = _find_tests332 loader.suiteClass = str333 suite = loader.discover('/foo/bar/baz', 'pattern', '/foo/bar')334 top_level_dir = os.path.abspath('/foo/bar')335 start_dir = os.path.abspath('/foo/bar/baz')336 self.assertEqual(suite, "['tests']")337 self.assertEqual(loader._top_level_dir, top_level_dir)338 self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])339 self.assertIn(top_level_dir, sys.path)340 def test_discover_start_dir_is_package_calls_package_load_tests(self):341 # This test verifies that the package load_tests in a package is indeed342 # invoked when the start_dir is a package (and not the top level).343 # http://bugs.python.org/issue22457344 # Test data: we expect the following:345 # an isfile to verify the package, then importing and scanning346 # as per _find_tests' normal behaviour.347 # We expect to see our load_tests hook called once.348 vfs = {abspath('/toplevel'): ['startdir'],349 abspath('/toplevel/startdir'): ['__init__.py']}350 def list_dir(path):351 return list(vfs[path])352 self.addCleanup(setattr, os, 'listdir', os.listdir)353 os.listdir = list_dir354 self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)355 os.path.isfile = lambda path: path.endswith('.py')356 self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)357 os.path.isdir = lambda path: not path.endswith('.py')358 self.addCleanup(sys.path.remove, abspath('/toplevel'))359 class Module(object):360 paths = []361 load_tests_args = []362 def __init__(self, path):363 self.path = path364 def load_tests(self, loader, tests, pattern):365 return ['load_tests called ' + self.path]366 def __eq__(self, other):367 return self.path == other.path368 loader = unittest.TestLoader()369 loader._get_module_from_name = lambda name: Module(name)370 loader.suiteClass = lambda thing: thing371 suite = loader.discover('/toplevel/startdir', top_level_dir='/toplevel')372 # We should have loaded tests from the package __init__.373 # (normally this would be nested TestSuites.)374 self.assertEqual(suite,375 [['load_tests called startdir']])376 def setup_import_issue_tests(self, fakefile):377 listdir = os.listdir378 os.listdir = lambda _: [fakefile]...
loader.py
Source:loader.py
...15def _make_failed_import_test(name, suiteClass):16 message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc())17 return _make_failed_test('ModuleImportFailure', name, ImportError(message),18 suiteClass)19def _make_failed_load_tests(name, exception, suiteClass):20 return _make_failed_test('LoadTestsFailure', name, exception, suiteClass)21def _make_failed_test(classname, methodname, exception, suiteClass):22 def testFailure(self):23 raise exception24 attrs = {methodname: testFailure}25 TestClass = type(classname, (case.TestCase,), attrs)26 return suiteClass((TestClass(methodname),))27class TestLoader(object):28 """29 This class is responsible for loading tests according to various criteria30 and returning them wrapped in a TestSuite31 """32 testMethodPrefix = 'test'33 sortTestMethodsUsing = cmp34 suiteClass = suite.TestSuite35 _top_level_dir = None36 def loadTestsFromTestCase(self, testCaseClass):37 """Return a suite of all tests cases contained in testCaseClass"""38 if issubclass(testCaseClass, suite.TestSuite):39 raise TypeError("Test cases should not be derived from TestSuite." \40 " Maybe you meant to derive from TestCase?")41 testCaseNames = self.getTestCaseNames(testCaseClass)42 if not testCaseNames and hasattr(testCaseClass, 'runTest'):43 testCaseNames = ['runTest']44 loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))45 return loaded_suite46 def loadTestsFromModule(self, module, use_load_tests=True):47 """Return a suite of all tests cases contained in the given module"""48 tests = []49 for name in dir(module):50 obj = getattr(module, name)51 if isinstance(obj, type) and issubclass(obj, case.TestCase):52 tests.append(self.loadTestsFromTestCase(obj))53 load_tests = getattr(module, 'load_tests', None)54 tests = self.suiteClass(tests)55 if use_load_tests and load_tests is not None:56 try:57 return load_tests(self, tests, None)58 except Exception, e:59 return _make_failed_load_tests(module.__name__, e,60 self.suiteClass)61 return tests62 def loadTestsFromName(self, name, module=None):63 """Return a suite of all tests cases given a string specifier.64 The name may resolve either to a module, a test case class, a65 test method within a test case class, or a callable object which66 returns a TestCase or TestSuite instance.67 The method optionally resolves the names relative to a given module.68 """69 parts = name.split('.')70 if module is None:71 parts_copy = parts[:]72 while parts_copy:73 try:74 module = __import__('.'.join(parts_copy))75 break76 except ImportError:77 del parts_copy[-1]78 if not parts_copy:79 raise80 parts = parts[1:]81 obj = module82 for part in parts:83 parent, obj = obj, getattr(obj, part)84 if isinstance(obj, types.ModuleType):85 return self.loadTestsFromModule(obj)86 elif isinstance(obj, type) and issubclass(obj, case.TestCase):87 return self.loadTestsFromTestCase(obj)88 elif (isinstance(obj, types.UnboundMethodType) and89 isinstance(parent, type) and90 issubclass(parent, case.TestCase)):91 return self.suiteClass([parent(obj.__name__)])92 elif isinstance(obj, suite.TestSuite):93 return obj94 elif hasattr(obj, '__call__'):95 test = obj()96 if isinstance(test, suite.TestSuite):97 return test98 elif isinstance(test, case.TestCase):99 return self.suiteClass([test])100 else:101 raise TypeError("calling %s returned %s, not a test" %102 (obj, test))103 else:104 raise TypeError("don't know how to make test from: %s" % obj)105 def loadTestsFromNames(self, names, module=None):106 """Return a suite of all tests cases found using the given sequence107 of string specifiers. See 'loadTestsFromName()'.108 """109 suites = [self.loadTestsFromName(name, module) for name in names]110 return self.suiteClass(suites)111 def getTestCaseNames(self, testCaseClass):112 """Return a sorted sequence of method names found within testCaseClass113 """114 def isTestMethod(attrname, testCaseClass=testCaseClass,115 prefix=self.testMethodPrefix):116 return attrname.startswith(prefix) and \117 hasattr(getattr(testCaseClass, attrname), '__call__')118 testFnNames = filter(isTestMethod, dir(testCaseClass))119 if self.sortTestMethodsUsing:120 testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))121 return testFnNames122 def discover(self, start_dir, pattern='test*.py', top_level_dir=None):123 """Find and return all test modules from the specified start124 directory, recursing into subdirectories to find them. Only test files125 that match the pattern will be loaded. (Using shell style pattern126 matching.)127 All test modules must be importable from the top level of the project.128 If the start directory is not the top level directory then the top129 level directory must be specified separately.130 If a test package name (directory with '__init__.py') matches the131 pattern then the package will be checked for a 'load_tests' function. If132 this exists then it will be called with loader, tests, pattern.133 If load_tests exists then discovery does *not* recurse into the package,134 load_tests is responsible for loading all tests in the package.135 The pattern is deliberately not stored as a loader attribute so that136 packages can continue discovery themselves. top_level_dir is stored so137 load_tests does not need to pass this argument in to loader.discover().138 """139 set_implicit_top = False140 if top_level_dir is None and self._top_level_dir is not None:141 # make top_level_dir optional if called from load_tests in a package142 top_level_dir = self._top_level_dir143 elif top_level_dir is None:144 set_implicit_top = True145 top_level_dir = start_dir146 top_level_dir = os.path.abspath(top_level_dir)147 if not top_level_dir in sys.path:148 # all test modules must be importable from the top level directory149 # should we *unconditionally* put the start directory in first150 # in sys.path to minimise likelihood of conflicts between installed151 # modules and development versions?152 sys.path.insert(0, top_level_dir)153 self._top_level_dir = top_level_dir154 is_not_importable = False155 if os.path.isdir(os.path.abspath(start_dir)):156 start_dir = os.path.abspath(start_dir)157 if start_dir != top_level_dir:158 is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))159 else:160 # support for discovery from dotted module names161 try:162 __import__(start_dir)163 except ImportError:164 is_not_importable = True165 else:166 the_module = sys.modules[start_dir]167 top_part = start_dir.split('.')[0]168 start_dir = os.path.abspath(os.path.dirname((the_module.__file__)))169 if set_implicit_top:170 self._top_level_dir = self._get_directory_containing_module(top_part)171 sys.path.remove(top_level_dir)172 if is_not_importable:173 raise ImportError('Start directory is not importable: %r' % start_dir)174 tests = list(self._find_tests(start_dir, pattern))175 return self.suiteClass(tests)176 def _get_directory_containing_module(self, module_name):177 module = sys.modules[module_name]178 full_path = os.path.abspath(module.__file__)179 if os.path.basename(full_path).lower().startswith('__init__.py'):180 return os.path.dirname(os.path.dirname(full_path))181 else:182 # here we have been given a module rather than a package - so183 # all we can do is search the *same* directory the module is in184 # should an exception be raised instead185 return os.path.dirname(full_path)186 def _get_name_from_path(self, path):187 path = os.path.splitext(os.path.normpath(path))[0]188 _relpath = os.path.relpath(path, self._top_level_dir)189 assert not os.path.isabs(_relpath), "Path must be within the project"190 assert not _relpath.startswith('..'), "Path must be within the project"191 name = _relpath.replace(os.path.sep, '.')192 return name193 def _get_module_from_name(self, name):194 __import__(name)195 return sys.modules[name]196 def _match_path(self, path, full_path, pattern):197 # override this method to use alternative matching strategy198 return fnmatch(path, pattern)199 def _find_tests(self, start_dir, pattern):200 """Used by discovery. Yields test suites it loads."""201 paths = os.listdir(start_dir)202 for path in paths:203 full_path = os.path.join(start_dir, path)204 if os.path.isfile(full_path):205 if not VALID_MODULE_NAME.match(path):206 # valid Python identifiers only207 continue208 if not self._match_path(path, full_path, pattern):209 continue210 # if the test file matches, load it211 name = self._get_name_from_path(full_path)212 try:213 module = self._get_module_from_name(name)214 except:215 yield _make_failed_import_test(name, self.suiteClass)216 else:217 mod_file = os.path.abspath(getattr(module, '__file__', full_path))218 realpath = os.path.splitext(mod_file)[0]219 fullpath_noext = os.path.splitext(full_path)[0]220 if realpath.lower() != fullpath_noext.lower():221 module_dir = os.path.dirname(realpath)222 mod_name = os.path.splitext(os.path.basename(full_path))[0]223 expected_dir = os.path.dirname(full_path)224 msg = ("%r module incorrectly imported from %r. Expected %r. "225 "Is this module globally installed?")226 raise ImportError(msg % (mod_name, module_dir, expected_dir))227 yield self.loadTestsFromModule(module)228 elif os.path.isdir(full_path):229 if not os.path.isfile(os.path.join(full_path, '__init__.py')):230 continue231 load_tests = None232 tests = None233 if fnmatch(path, pattern):234 # only check load_tests if the package directory itself matches the filter235 name = self._get_name_from_path(full_path)236 package = self._get_module_from_name(name)237 load_tests = getattr(package, 'load_tests', None)238 tests = self.loadTestsFromModule(package, use_load_tests=False)239 if load_tests is None:240 if tests is not None:241 # tests loaded from package file242 yield tests243 # recurse into the package244 for test in self._find_tests(full_path, pattern):245 yield test246 else:247 try:248 yield load_tests(self, tests, pattern)249 except Exception, e:250 yield _make_failed_load_tests(package.__name__, e,251 self.suiteClass)252defaultTestLoader = TestLoader()253def _makeLoader(prefix, sortUsing, suiteClass=None):254 loader = TestLoader()255 loader.sortTestMethodsUsing = sortUsing256 loader.testMethodPrefix = prefix257 if suiteClass:258 loader.suiteClass = suiteClass259 return loader260def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp):261 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)262def makeSuite(testCaseClass, prefix='test', sortUsing=cmp,263 suiteClass=suite.TestSuite):264 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)...
script_load_class.py
Source:script_load_class.py
1#=======================================================================================================================2# СкÑÐ¸Ð¿Ñ ÑлÑÐ¶Ð¸Ñ Ð´Ð»Ñ Ð·Ð°Ð¿ÑÑка набоÑа ÑеÑÑов Jmeter запиÑаннÑÑ
в ÑпиÑов scenarios (пÑÑÑÑ ÑказÑваÑÑ Ð¿Ð¾Ð»Ð½Ñй до пÑоекÑа).3# ÐейÑÑвиÑ:4# 1) Ð¡Ð¾Ð·Ð´Ð°ÐµÑ Ð´Ð¸ÑекÑоÑÐ¸Ñ Ñ ÑекÑÑей даÑой и вÑеменем, копиÑÑÐµÑ Ð² Ð½ÐµÑ Ð½ÐµÐ¾Ð±Ñ
одимÑе даннÑе Ð´Ð»Ñ Ð·Ð°Ð¿ÑÑка.5# 2) ÐапÑÑÐºÐ°ÐµÑ Ð¿Ð¾Ð¾ÑеÑдно вÑе пÑоекÑÑ Ð¿Ð¾ два Ñаза Ñ Ð·Ð°Ð´ÐµÑжкой 600 Ñек Ð¼ÐµÐ¶Ð´Ñ Ð·Ð°Ð¿ÑÑками.6# 3) СÑÑÐ¾Ð¸Ñ Ð²Ñе необÑ
одимÑе оÑÑеÑÑ Ð¸ гÑаÑики.7# 4) ÐÑÑ
ивиÑÑÐµÑ Ð²Ñе ÑодеÑжимое папки в zip аÑÑ
ив.8# 5) УдалÑÐµÑ Ð²Ñе даннÑе полÑÑеннÑе в Ñ
оде ÑеÑÑиÑованиÑ: jtl, log, txt.9#=======================================================================================================================10# -*- coding: utf-8 -*-11import subprocess12import os13import time14import shutil15import csv16from xlsxwriter.workbook import Workbook17#СпиÑок пÑоекÑов Ð´Ð»Ñ Ð·Ð°Ð¿ÑÑка18scenarios = [19 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.ÐÑбоÑка\test.jmx',20 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.ÐапиÑÑÐоÐодÑ\test.jmx',21 r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.ÐайÑиÐакÑималÑноÐоÑ
ожийÐкаÑо\test.jmx',22 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.СпиÑок\test.jmx',23 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.СпиÑокÐлÑÐвÑодополнениÑ\test.jmx',24 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.СпиÑокÐоÐолномÑÐодÑ\test.jmx',25 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.СпиÑокРегионов\test.jmx',26 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.СпиÑокРегионовСÐоÑодами\test.jmx',27 #r'c:\inetpub\load_tests\ÐлаÑÑиÑикаÑоÑÑ\ÐлаÑÑиÑикаÑоÑÑ.ÐолÑÑиÑÑСпÑавоÑникЦеликом\test.jmx', 28 ]29#ÐÑÐµÐ¼Ñ Ð¿ÑоÑÑÐ¾Ñ Ð¼ÐµÐ¶Ð´Ñ ÑеÑÑами30time_delay = 131#ÐÑÑÑ Ðº Jmeter32Jmeter_path = r'c:\JMeter\bin\ApacheJMeter.jar'33#ÐÑÑÑ Ðº Ð¿Ð»Ð°Ð³Ð¸Ð½Ñ Ð´Ð»Ñ Ð¿Ð¾ÑÑÑÐ¾ÐµÐ½Ð¸Ñ Ð¾ÑÑеÑов34CMDRunner_path = r'c:\JMeter\lib\ext\CMDRunner.jar'35#ÐÑÑÑ Ðº аÑÑ
иваÑоÑÑ 7-zip36Arch_path = r'c:\7-Zip\7z.exe'37new_scenarios = []38filter_file = []39def run_and_copy(scenarios):40 '''ÐопиÑÑÐµÑ ÑÐ°Ð¹Ð»Ñ Ð¿ÑоекÑа41 42 Ð¡Ð¾Ð·Ð´Ð°ÐµÑ Ð´Ð¸ÑекÑоÑÐ¸Ñ Ð² папке Ñ Ð¿ÑоекÑом Ñ ÑекÑÑей даÑой и вÑеменем. 43 ÐопиÑÑÐµÑ Ð² Ð½ÐµÑ Ð²Ñе необÑ
одимÑе ÑÐ°Ð¹Ð»Ñ Ð´Ð»Ñ Ð·Ð°Ð¿ÑÑка.44 ÐапÑÑÐºÐ°ÐµÑ ÑеÑÑ.45 ÐадеÑжка Ð¼ÐµÐ¶Ð´Ñ Ð·Ð°Ð¿ÑÑками 600 Ñек.46 ÐозвÑаÑÐ°ÐµÑ Ð½Ð¾Ð²Ñй ÑпиÑок Ñ Ð¿ÑÑÑми Ð´Ð»Ñ Ð·Ð°Ð¿ÑÑка ÑеÑÑов.47 48 '''49 50 for i in range(1):51 for scenario in scenarios:52 filter_file = []53 os.chdir(os.path.dirname(scenario)) 54 files = os.listdir(os.path.dirname(scenario))55 for file in files:56 if (file.endswith('.jtl') == file.endswith('.txt') == file.endswith('.log') == False and os.path.isfile(file) == True):57 filter_file.append(file) 58 dir_name = time.strftime("%d%m%y_%H%M%S", time.localtime())59 os.mkdir(dir_name) 60 for file in filter_file:61 shutil.copyfile(os.path.join(os.path.dirname(scenario), file), os.path.join(os.path.dirname(scenario), dir_name, file))62 (folder, file_name) = os.path.split(scenario)63 new_scenarios.append(os.path.join(folder, dir_name, file_name))64 os.chdir(os.path.join(folder, dir_name))65 mon = subprocess.Popen(['python','test_monitor.py'])66 subprocess.call(['java', '-jar', Jmeter_path, '-n', '-t', os.path.join(folder, dir_name, file_name)])67 subprocess.Popen.terminate(mon)68 time.sleep(time_delay)69 return new_scenarios 70 71def build_report(scenarios):72 "СÑÑÐ¾Ð¸Ñ Ð¾ÑÑеÑÑ Ð¸Ð· полÑÑеннÑÑ
даннÑÑ
"73 74 for scenario in scenarios:75 os.chdir(os.path.dirname(scenario))76 77 subprocess.call(['java', '-jar', CMDRunner_path, "--tool", "Reporter" 78 ,"--generate-png", "response_times_vs_threads.png", '--input-jtl', r"{0}\response_times_vs_threads.jtl"79 .format(os.path.dirname(scenario)),80 '--plugin-type', 'TimesVsThreads', '--exclude-labels', 'ÐвÑоÑизаÑÐ¸Ñ Ð½Ð° ÑайÑе','--width', '1200', '--height', '900'])81 subprocess.call(['java', '-jar', CMDRunner_path, "--tool", "Reporter"82 ,"--generate-png", "response_times_over_time.png", '--input-jtl', r"{0}\response_times_vs_threads.jtl"83 .format(os.path.dirname(scenario)),84 '--plugin-type', 'ResponseTimesOverTime', '--exclude-labels', 'ÐвÑоÑизаÑÐ¸Ñ Ð½Ð° ÑайÑе', '--width', '1200', '--height', '900']) 85 86 subprocess.call(['java', '-jar', CMDRunner_path, "--tool", "Reporter" 87 ,"--generate-png", "response_codes_per_second.png", '--exclude-labels', 'ÐвÑоÑизаÑÐ¸Ñ Ð½Ð° ÑайÑе', '--input-jtl', r"{0}\response_times_vs_threads.jtl"88 .format(os.path.dirname(scenario)),89 '--plugin-type', 'ResponseCodesPerSecond', '--width', '1200', '--height', '900'])90 91 subprocess.call(['java', '-jar', CMDRunner_path, "--tool", "Reporter" 92 ,"--generate-csv", "summary.csv", '--input-jtl', r"{0}\response_times_vs_threads.jtl"93 .format(os.path.dirname(scenario)),94 '--plugin-type', 'AggregateReport'])95 96 97def archive_data(scenarios):98 "ÐÑÑ
ивиÑÑÐµÑ Ð²Ñе ÑодеÑжимое папки в аÑÑ
ив Ñ Ð½Ð°Ð·Ð²Ð°Ð½Ð¸Ðµ диÑекÑоÑии"99 100 for scenario in scenarios:101 os.chdir(os.path.dirname(scenario))102 path = scenario.split(os.sep)103 zip_dir = path[len(path)-2]104 zip_path = os.path.join(os.path.dirname(scenario), zip_dir + '.zip')105 subprocess.check_call([Arch_path, "a", "-tzip", "-ssw", "-mx4", zip_path, os.path.dirname(scenario)])106def delete_data(scenarios):107 "УдалÑÐµÑ Ð²Ñе .jtl, .log, .txt из папки"108 109 for scenario in scenarios:110 os.chdir(os.path.dirname(scenario))111 files = os.listdir(os.path.dirname(scenario))112 for file in files:113 if (file.endswith('.jtl') == True or file.endswith('.txt') == True or file.endswith('.log') == True):114 os.remove(file)115 116def export_exel(scenarios):117 "СÑÑÐ¾Ð¸Ñ exel оÑÑÐµÑ Ð¿Ð¾ ÑеÑÑÑ"118 119 metrics = [120 'ÐбÑее кол-во запÑоÑов',121 'СÑеднее вÑÐµÐ¼Ñ Ð¾Ñклика, мÑ',122 'median',123 '90% line',124 'ÐинималÑное вÑÐµÐ¼Ñ Ð¾Ñклика, мÑ',125 'ÐакÑималÑное вÑÐµÐ¼Ñ Ð¾Ñклика, мÑ',126 'ÐÑоÑÐµÐ½Ñ Ð¾Ñибок',127 'Ðол-во запÑоÑов в ÑекÑндÑ',128 'ÐÑопÑÑÐºÐ½Ð°Ñ ÑпоÑобноÑÑÑ, Kb/sev',129 'СÑед.квадÑаÑиÑное оÑклонение'130 ] 131 132 reports = ([133 'response_codes_per_second.png',134 'response_times_over_time.png',135 'response_times_vs_threads.png'136 ],137 [138 'Ðзменение колиÑеÑÑва кодов в ÑекÑÐ½Ð´Ñ Ð² Ñ
оде иÑпÑÑаниÑ',139 'Ðзменение вÑемени оÑвеÑа в Ñ
оде иÑпÑÑаниÑ',140 'Ðзменение вÑемени оÑвеÑа в завиÑимоÑÑи Ð¾Ñ ÐºÐ¾Ð»-ва полÑзоваÑелей',141 ])142 i = 0143 test_number = 1144 work_doc = Workbook('report.xlsx')145 sheet = work_doc.add_worksheet(time.strftime("%d%m%y", time.localtime()))146 sheet.set_column(0, 0, 35)147 sheet.set_column(1, 1, 15)148 for scenario in scenarios:149 os.chdir(os.path.dirname(scenario))150 with open ('summary.csv','r') as f:151 reader = csv.reader(f, delimiter = ',')152 parameters = list(reader) 153 154 sheet.write(i, 0, 'ТеÑÑ â%s. %s' % (test_number, parameters[2][0]))155 sheet.write(i+2, 0, 'ÐеÑÑика')156 sheet.write(i+2, 1, 'РезÑлÑÑаÑ') 157 158 for a in range(len(metrics)):159 sheet.write(i+a+3, 0, metrics[a])160 sheet.write(i+a+3, 1, parameters[2][a+1])161 162 i += 16163 164 for a in range(len(reports[0])):165 sheet.insert_image(i, 0, reports[0][a], {'x_scale': 0.5, 'y_scale': 0.5})166 sheet.write(i+23, 0, 'РиÑ.%s.%s. %s' % (test_number, a+1, reports[1][a]))167 i += 28 168 test_number +=1169 170 sheet.write(i, 0, 'ÐÑводÑ')171 work_doc.close()172 173new_scenarios = run_and_copy(scenarios)174build_report(new_scenarios)175archive_data(new_scenarios)176delete_data(new_scenarios)...
test_templates.py
Source:test_templates.py
...3from bgpcfgd.template import TemplateFabric4from bgpcfgd.config import ConfigMgr5from .util import load_constants_dir_mappings6TEMPLATE_PATH = os.path.abspath('../../dockers/docker-fpm-frr/frr')7def load_tests(peer_type, template_name):8 constants = load_constants_dir_mappings()9 path = "tests/data/%s/%s" % (constants[peer_type], template_name)10 param_files = [name for name in os.listdir(path)11 if os.path.isfile(os.path.join(path, name)) and name.startswith("param_")]12 tests = []13 for param_fname in param_files:14 casename = param_fname.replace("param_", "").replace(".json", "")15 result_fname = "result_%s.conf" % casename16 full_param_fname = os.path.join(path, param_fname)17 full_result_fname = os.path.join(path, result_fname)18 tests.append((casename, full_param_fname, full_result_fname))19 tmpl_path = os.path.join("bgpd", "templates", constants[peer_type], "%s.j2" % template_name)20 return tmpl_path, tests21def load_json(fname):22 with open(fname) as param_fp:23 raw_params = json.load(param_fp)24 params = {}25 for table_key, table_entries in raw_params.items():26 if table_key.startswith("CONFIG_DB__"):27 # convert CONFIG_DB__* entries keys into tuple if needed28 new_table_entries = {}29 for entry_key, entry_value in table_entries.items():30 if '|' in entry_key:31 new_key = tuple(entry_key.split('|'))32 else:33 new_key = entry_key34 new_table_entries[new_key] = entry_value35 params[table_key] = new_table_entries36 else:37 params[table_key] = table_entries38 return params39def compress_comments(raw_config):40 comment_counter = 041 output = []42 for line in raw_config.split('\n'):43 stripped_line = line.strip()44 # Skip empty lines45 if stripped_line == '':46 pass47 # Write lines without comments48 elif not stripped_line.startswith('!'):49 if comment_counter > 0:50 output.append("!")51 comment_counter = 052 output.append(line)53 # Write non-empty comments54 elif stripped_line.startswith('!') and len(stripped_line) > 1:55 if comment_counter > 0:56 output.append("!")57 comment_counter = 058 output.append(line)59 # Count empty comments60 else: # stripped_line == '!'61 comment_counter += 162 # Flush last comment if we have one63 if comment_counter > 0:64 output.append("!")65 return "\n".join(output) + "\n"66def write_result(fname, raw_result):67 with open(fname, 'w') as fp:68 raw_result_w_commpressed_comments = compress_comments(raw_result)69 fp.write(raw_result_w_commpressed_comments)70def run_tests(test_name, template_fname, tests):71 tf = TemplateFabric(TEMPLATE_PATH)72 template = tf.from_file(template_fname)73 for case_name, param_fname, result_fname in tests:74 params = load_json(param_fname)75 raw_generated_result = str(template.render(params))76 assert "None" not in raw_generated_result, "Test %s.%s" % (test_name, case_name)77 # this is used only for initial generation write_result(result_fname, raw_generated_result)78 canonical_generated_result = ConfigMgr.to_canonical(raw_generated_result)79 with open(result_fname) as result_fp:80 raw_saved_result = result_fp.read()81 canonical_saved_result = ConfigMgr.to_canonical(raw_saved_result)82 assert canonical_saved_result == canonical_generated_result, "Test %s.%s" % (test_name, case_name)83# Tests84def test_general_policies():85 test_data = load_tests("general", "policies.conf")86 run_tests("general_policies", *test_data)87def test_general_pg():88 test_data = load_tests("general", "peer-group.conf")89 run_tests("general_pg", *test_data)90def test_general_instance():91 test_data = load_tests("general", "instance.conf")92 run_tests("general_instance", *test_data)93def test_internal_policies():94 test_data = load_tests("internal", "policies.conf")95 run_tests("internal_policies", *test_data)96def test_internal_pg():97 test_data = load_tests("internal", "peer-group.conf")98 run_tests("internal_pg", *test_data)99def test_internal_instance():100 test_data = load_tests("internal", "instance.conf")101 run_tests("internal_instance", *test_data)102def test_dynamic_policies():103 test_data = load_tests("dynamic", "policies.conf")104 run_tests("dynamic_policies", *test_data)105def test_dynamic_pg():106 test_data = load_tests("dynamic", "peer-group.conf")107 run_tests("dynamic_pg", *test_data)108def test_dynamic_instance():109 test_data = load_tests("dynamic", "instance.conf")110 run_tests("dynamic_instance", *test_data)111def test_monitors_policies():112 test_data = load_tests("monitors", "policies.conf")113 run_tests("monitors_policies", *test_data)114def test_monitors_pg():115 test_data = load_tests("monitors", "peer-group.conf")116 run_tests("monitors_pg", *test_data)117def test_monitors_instance():118 test_data = load_tests("monitors", "instance.conf")119 run_tests("monitors_instance", *test_data)120def test_voq_chassis_policies():121 test_data = load_tests("voq_chassis", "policies.conf")122 run_tests("voq_chassis_policies", *test_data)123def test_voq_chassis_pg():124 test_data = load_tests("voq_chassis", "peer-group.conf")125 run_tests("voq_chassis_pg", *test_data)126def test_voq_chassis_instance():127 test_data = load_tests("voq_chassis", "instance.conf")...
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!!