def loadTests(self): loader = TestLoader() discover = unittest.defaultTestLoader.discover( "./testCases", pattern='*.py') #RechargeCases WithdrawCases PasswdManageCase #discover=unittest.defaultTestLoader.discover("./123", pattern='*.py') suite = unittest.TestSuite() testlist = discover._tests testlist = list(testlist) testCasesDict = {} testClassDict = {} for test in testlist: for x in test: for y in x: yy = dir(y.__class__) tempList = [] for y1 in yy: if y1.startswith("test"): print y1, y.__class__ tempList.append(y1) else: continue testClassDict[str(y.__class__)] = y.__class__ testCasesDict[str(y.__class__)] = tempList break for k in sorted(testClassDict.iterkeys()): for testValue in testCasesDict[k]: print testClassDict[k], testValue suite.addTest(testClassDict[k](testValue)) return suite
def load_tests_from_classes(test_cases): suite = TestSuite() loader = TestLoader() for test_class in test_cases: tests = loader.loadTestsFromTestCase(test_class) suite.addTests(tests) return suite
def main(): with app.app_context(): if os.getenv('FLASK_ENV') not in ['testing']: raise Exception('TESTS IS ALLOWED TO RUN ONLY END TESTING MODE') db.create_all() suite = TestLoader().discover( 'tests', pattern='test_*.py', top_level_dir=os.environ['PYTHONPATH'].split(os.pathsep)[0] ) return TextTestRunner(verbosity=1).run(suite)
def test_package(*package_names): tests = [] test_loader = TestLoader() for module_name, module in sys.modules.items(): for package_name in package_names: if module_name.startswith('{}.'.format(package_name)): module_tests = test_loader.loadTestsFromModule(module) module_tests = [ t for t in module_tests if is_test_suite_loaded(t) ] tests.extend(module_tests) break test_result = TextTestRunner(failfast=True, resultclass=TimedTextTestResult).run( TestSuite(tests)) if not test_result.wasSuccessful(): raise Exception('test failed')
def __init__(self): ''' Constructor ''' parser = ArgumentParser(epilog = 'ACTC Test Runner') parser.add_argument('-c', '--coverage', action = 'store_true', help = 'activate code coverage measurement') args = parser.parse_args() if (args.coverage): from coverage import coverage cov = coverage(include = 'src/*', omit = '*/test/*.py') # actc.cli cov.exclude('Exception') cov.exclude('parser.error') # actc.dodo cov.exclude('NotImplementedError') cov.start() # end if TextTestRunner(verbosity = 2).run(TestLoader().discover('src')) if (args.coverage): cov.stop() cov.html_report(directory = 'coverage', title = 'ACTC code coverage') print('See coverage/index.html report')
''' 方式一:通过TestSuite类 ''' # 实例化一个TestSuite对象 # suite = TestSuite() # 通过addTest(test)方法,test参数格式是:测试用例类名('用例方法名') # suite.addTest(TestRegister('test_01')) # suite.addTest(TestFriendlyLink('test_01')) # 通过addTests(tests)方法,tests代表的是含有单元测试脚本的可迭代对象 # suite.addTests([TestRegister('test_01'), TestFriendlyLink('test_01'), # TestFriendlyLink('test_02')]) ''' 方式二:通过TestLoader类 ''' # 实例化一个TestLoader对象 loader = TestLoader() # 根据测试用例类名进行加载 # suite = loader.loadTestsFromTestCase(TestFriendlyLink) # 根据指明的模块名进行加载 # suite = loader.loadTestsFromModule(t02_friendly_link) # 通过discover方法:根据指定的工程目录下文件(模块)的命名规则来进行自动扫描加载测试用例,返回值为:TestSuite对象 # test*.py 指的是: # 1、先加载包的配置文件中已经声明的模块中符合规则的测试用例: # (第一必须继承TestCase类,第二必须有以test开头的方法)(如果没有符合规则的,会取得内容为空 的suite对象) # 2、再去扫描所有以test开头的模块,并加载用例(如果没有符合规则的,则不录入,就是不收集起来) project_path = os.path.dirname(os.getcwd()) suite = loader.discover(project_path, "test*.py") # 第二部分:执行脚本 ''' 方式一:通过TextTestRunner类
import os import sys import argparse from unittest.loader import TestLoader from unittest.runner import TextTestRunner from pmworker import setup_logging BASE_DIR = os.path.dirname(os.path.abspath(__file__)) test_loader = TestLoader() test_runner = TextTestRunner() os.environ['CELERY_CONFIG_MODULE'] = 'pmworker.config.test' setup_logging() parser = argparse.ArgumentParser() parser.add_argument('-p', '--pattern', default='test*py', help='Test files pattern.') args = parser.parse_args() if args.pattern.endswith('py'): discovery_pattern = args.pattern else: discovery_pattern = args.pattern + "*"
def main(): suite = TestLoader().discover(".") unittest.TextTestRunner().run(suite)
from unittest_timing import TimingTestRunner from unittest.loader import TestLoader suite = TestLoader().discover('apps') runner = TimingTestRunner() runner.run(suite)
def load_tests(loader, tests, pattern): #pylint: disable=W0613 ''' Returns all of the testcases in this module as a testsuite ''' return TestLoader().loadTestsFromTestCase(TestBookData)
def __init__(self): self.loader = TestLoader()
def suite(): suite = TestLoader().loadTestsFromNames( ['specific_argparse_poslessed', 'specific_posless']) return suite
def suite(): suite = TestLoader().loadTestsFromNames( ['testbxilog', 'testparserconf', 'testremotehandler']) return suite
def load_tests(loader, tests, pattern): ''' Returns all of the testcases in this module as a testsuite ''' return TestLoader().loadTestsFromTestCase(TestUtils)
import sys import unittest import cv2 import numpy as np import threading from PIL import ImageGrab from functools import wraps from time import sleep, time from appium.webdriver.common.mobileby import MobileBy as By from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support.ui import WebDriverWait from unittest.loader import TestLoader from HtmlTestRunner.result import _HtmlTestResult as htmlResult from globalvar import dependency, ui, info, desireCap from appium import webdriver getTestCase = TestLoader().getTestCaseNames class fetchyaml(object): def locate_method(self, method): if method == 'ID': return By.ID elif method == 'XPATH': return By.XPATH elif method == 'CLASSNAME': return By.CLASS_NAME elif method == 'ANDROID_UI': return By.ANDROID_UIAUTOMATOR elif method == 'IOS_PREDICATE': return By.IOS_PREDICATE elif method == 'ACCESS_ID':