Best Python code snippet using tempest_python
test_script.py
Source:test_script.py
1import inspect, StringIO, sys, sets2from twisted.trial import unittest3from twisted.scripts import trial4from twisted.python import util, usage5def sibpath(filename):6 """For finding files in twisted/trial/test"""7 return util.sibpath(__file__, filename)8class TestModuleTest(unittest.TestCase):9 def setUp(self):10 self.config = trial.Options()11 def tearDown(self):12 self.config = None13 def test_baseState(self):14 self.failUnlessEqual(0, len(self.config['tests']))15 def test_testmoduleOnModule(self):16 self.config.opt_testmodule(sibpath('moduletest.py'))17 self.failUnlessEqual('twisted.trial.test.test_test_visitor',18 self.config['tests'][0])19 def test_testmoduleOnScript(self):20 self.config.opt_testmodule(sibpath('scripttest.py'))21 self.failUnlessEqual(sets.Set(['twisted.trial.test.test_test_visitor',22 'twisted.trial.test.test_class']),23 sets.Set(self.config['tests']))24 def test_testmoduleOnNonexistentFile(self):25 buffy = StringIO.StringIO()26 stderr, sys.stderr = sys.stderr, buffy27 filename = 'test_thisbetternoteverexist.py'28 try:29 self.config.opt_testmodule(filename)30 self.failUnlessEqual([], self.config['tests'])31 self.failUnlessEqual("File %r doesn't exist\n" % (filename,),32 buffy.getvalue())33 finally:34 sys.stderr = stderr35 def test_testmoduleOnEmptyVars(self):36 self.config.opt_testmodule(sibpath('novars.py'))37 self.failUnlessEqual([], self.config['tests'])38 def test_testmoduleOnModuleName(self):39 buffy = StringIO.StringIO()40 stderr, sys.stderr = sys.stderr, buffy41 moduleName = 'twisted.trial.test.test_script'42 try:43 self.config.opt_testmodule(moduleName)44 self.failUnlessEqual([], self.config['tests'])45 self.failUnlessEqual("File %r doesn't exist\n" % (moduleName,),46 buffy.getvalue())47 finally:48 sys.stderr = stderr49 def test_parseLocalVariable(self):50 declaration = '-*- test-case-name: twisted.trial.test.test_tests -*-'51 localVars = trial._parseLocalVariables(declaration)52 self.failUnlessEqual({'test-case-name':53 'twisted.trial.test.test_tests'},54 localVars)55 def test_trailingSemicolon(self):56 declaration = '-*- test-case-name: twisted.trial.test.test_tests; -*-'57 localVars = trial._parseLocalVariables(declaration)58 self.failUnlessEqual({'test-case-name':59 'twisted.trial.test.test_tests'},60 localVars)61 62 def test_parseLocalVariables(self):63 declaration = ('-*- test-case-name: twisted.trial.test.test_tests; ' 64 'foo: bar -*-')65 localVars = trial._parseLocalVariables(declaration)66 self.failUnlessEqual({'test-case-name':67 'twisted.trial.test.test_tests',68 'foo': 'bar'},69 localVars)70 def test_surroundingGuff(self):71 declaration = ('## -*- test-case-name: '72 'twisted.trial.test.test_tests -*- #')73 localVars = trial._parseLocalVariables(declaration)74 self.failUnlessEqual({'test-case-name':75 'twisted.trial.test.test_tests'},76 localVars)77 def test_invalidLine(self):78 self.failUnlessRaises(ValueError, trial._parseLocalVariables,79 'foo')80 def test_invalidDeclaration(self):81 self.failUnlessRaises(ValueError, trial._parseLocalVariables,82 '-*- foo -*-')83 self.failUnlessRaises(ValueError, trial._parseLocalVariables,84 '-*- foo: bar; qux -*-')85 self.failUnlessRaises(ValueError, trial._parseLocalVariables,86 '-*- foo: bar: baz; qux: qax -*-')87 def test_variablesFromFile(self):88 localVars = trial.loadLocalVariables(sibpath('moduletest.py'))89 self.failUnlessEqual({'test-case-name':90 'twisted.trial.test.test_test_visitor'},91 localVars)92 93 def test_noVariablesInFile(self):94 localVars = trial.loadLocalVariables(sibpath('novars.py'))95 self.failUnlessEqual({}, localVars)96 def test_variablesFromScript(self):97 localVars = trial.loadLocalVariables(sibpath('scripttest.py'))98 self.failUnlessEqual(99 {'test-case-name': ('twisted.trial.test.test_test_visitor,'100 'twisted.trial.test.test_class')},101 localVars)102 def test_getTestModules(self):103 modules = trial.getTestModules(sibpath('moduletest.py'))104 self.failUnlessEqual(modules, ['twisted.trial.test.test_test_visitor'])105 def test_getTestModules_noVars(self):106 modules = trial.getTestModules(sibpath('novars.py'))107 self.failUnlessEqual(len(modules), 0)108 def test_getTestModules_multiple(self):109 modules = trial.getTestModules(sibpath('scripttest.py'))110 self.failUnlessEqual(sets.Set(modules),111 sets.Set(['twisted.trial.test.test_test_visitor',112 'twisted.trial.test.test_class']))113 def test_looksLikeTestModule(self):114 for filename in ['test_script.py', 'twisted/trial/test/test_script.py']:115 self.failUnless(trial.isTestFile(filename),116 "%r should be a test file" % (filename,))117 for filename in ['twisted/trial/test/moduletest.py',118 sibpath('scripttest.py'), sibpath('test_foo.bat')]:119 self.failIf(trial.isTestFile(filename),...
linecount.py
Source:linecount.py
1#!/usr/bin/env python2#3# __COPYRIGHT__4#5# Count statistics about SCons test and source files. This must be run6# against a fully-populated tree (for example, one that's been freshly7# checked out).8#9# A test file is anything under the src/ directory that begins with10# 'test_' or ends in 'Tests.py', or anything under the test/ directory11# that ends in '.py'. Note that runtest.py script does *not*, by default,12# consider the files that begin with 'test_' to be tests, because they're13# tests of SCons packaging and installation, not functional tests of14# SCons code.15#16# A source file is anything under the src/engine/ or src/script/17# directories that ends in '.py' but does NOT begin with 'test_'18# or end in 'Tests.py'.19#20# We report the number of tests and sources, the total number of lines21# in each category, the number of non-blank lines, and the number of22# non-comment lines. The last figure (non-comment) lines is the most23# interesting one for most purposes.24from __future__ import division25__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"26import os.path27fmt = "%-16s %5s %7s %9s %11s %11s"28class Collection(object):29 def __init__(self, name, files=None, pred=None):30 self._name = name31 if files is None:32 files = []33 self.files = files34 if pred is None:35 pred = lambda x: True36 self.pred = pred37 def __call__(self, fname):38 return self.pred(fname)39 def __len__(self):40 return len(self.files)41 def collect(self, directory):42 for dirpath, dirnames, filenames in os.walk(directory):43 try: dirnames.remove('.svn')44 except ValueError: pass45 self.files.extend([ os.path.join(dirpath, f)46 for f in filenames if self.pred(f) ])47 def lines(self):48 try:49 return self._lines50 except AttributeError:51 self._lines = lines = []52 for file in self.files:53 file_lines = open(file).readlines()54 lines.extend([s.lstrip() for s in file_lines])55 return lines56 def non_blank(self):57 return [s for s in self.lines() if s != '']58 def non_comment(self):59 return [s for s in self.lines() if s == '' or s[0] != '#']60 def non_blank_non_comment(self):61 return [s for s in self.lines() if s != '' and s[0] != '#']62 def printables(self):63 return (self._name + ':',64 len(self.files),65 len(self.lines()),66 len(self.non_blank()),67 len(self.non_comment()),68 len(self.non_blank_non_comment()))69def is_Tests_py(x):70 return x[-8:] == 'Tests.py'71def is_test_(x):72 return x[:5] == 'test_'73def is_python(x):74 return x[-3:] == '.py'75def is_source(x):76 return is_python(x) and not is_Tests_py(x) and not is_test_(x)77src_Tests_py_tests = Collection('src/ *Tests.py', pred=is_Tests_py)78src_test_tests = Collection('src/ test_*.py', pred=is_test_)79test_tests = Collection('test/ tests', pred=is_python)80sources = Collection('sources', pred=is_source)81src_Tests_py_tests.collect('src')82src_test_tests.collect('src')83test_tests.collect('test')84sources.collect('src/engine')85sources.collect('src/script')86src_tests = Collection('src/ tests', src_Tests_py_tests.files87 + src_test_tests.files)88all_tests = Collection('all tests', src_tests.files + test_tests.files)89def ratio(over, under):90 return "%.2f" % (float(len(over)) / float(len(under)))91print fmt % ('', '', '', '', '', 'non-blank')92print fmt % ('', 'files', 'lines', 'non-blank', 'non-comment', 'non-comment')93print94print fmt % src_Tests_py_tests.printables()95print fmt % src_test_tests.printables()96print97print fmt % src_tests.printables()98print fmt % test_tests.printables()99print100print fmt % all_tests.printables()101print fmt % sources.printables()102print103print fmt % ('ratio:',104 ratio(all_tests, sources),105 ratio(all_tests.lines(), sources.lines()),106 ratio(all_tests.non_blank(), sources.non_blank()),107 ratio(all_tests.non_comment(), sources.non_comment()),108 ratio(all_tests.non_blank_non_comment(),109 sources.non_blank_non_comment())...
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!!