def test(calculators=[], jobs=0, stream=sys.stdout, files=None, verbose=False): """Main test-runner for ASE.""" if LooseVersion(np.__version__) >= '1.14': # Our doctests need this (spacegroup.py) np.set_printoptions(legacy='1.13') test_calculator_names.extend(calculators) disable_calculators([name for name in calc_names if name not in calculators]) tests = get_tests(files) if len(set(tests)) != len(tests): # Since testsubdirs are based on test name, we will get race # conditions on IO if the same test runs more than once. print('Error: One or more tests specified multiple times', file=sys.stderr) sys.exit(1) if jobs == -1: # -1 == auto jobs = min(cpu_count(), len(tests), 32) print_info() origcwd = os.getcwd() testdir = tempfile.mkdtemp(prefix='ase-test-') os.chdir(testdir) # Note: :25 corresponds to ase.cli indentation print('{:25}{}'.format('test directory', testdir)) if test_calculator_names: print('{:25}{}'.format('Enabled calculators:', ' '.join(test_calculator_names))) print('{:25}{}'.format('number of processes', jobs or '1 (multiprocessing disabled)')) print('{:25}{}'.format('time', time.strftime('%c'))) print() t1 = time.time() results = [] try: for result in runtests_parallel(jobs, tests, verbose): results.append(result) except KeyboardInterrupt: print('Interrupted by keyboard') return 1 else: summary(results) ntrouble = len([r for r in results if r.status in ['FAIL', 'ERROR']]) return ntrouble finally: t2 = time.time() print('Time elapsed: {:.1f} s'.format(t2 - t1)) os.chdir(origcwd)
def test(verbosity=1, calculators=[], testdir=None, stream=sys.stdout, files=None): test_calculator_names.extend(calculators) disable_calculators( [name for name in calc_names if name not in calculators]) tests = get_tests(files) ts = unittest.TestSuite() for test in tests: ts.addTest(ScriptTestCase(filename=os.path.abspath(test))) if verbosity > 0: print_info() sys.stdout = devnull if verbosity == 0: stream = devnull ttr = unittest.TextTestRunner(verbosity=verbosity, stream=stream) origcwd = os.getcwd() if testdir is None: testdir = tempfile.mkdtemp(prefix='ase-test-') else: if os.path.isdir(testdir): shutil.rmtree(testdir) # clean before running tests! os.mkdir(testdir) os.chdir(testdir) if verbosity: print('test-dir ', testdir, '\n', file=sys.__stdout__) try: results = ttr.run(ts) finally: os.chdir(origcwd) sys.stdout = sys.__stdout__ return results
def test(verbosity=1, calculators=[], stream=sys.stdout, files=None): """Main test-runner for ASE.""" if LooseVersion(np.__version__) >= '1.14': # Our doctests need this (spacegroup.py) np.set_printoptions(legacy='1.13') test_calculator_names.extend(calculators) disable_calculators( [name for name in calc_names if name not in calculators]) tests = get_tests(files) ts = unittest.TestSuite() for test in tests: ts.addTest(ScriptTestCase(filename=os.path.abspath(test))) if verbosity > 0: print_info() sys.stdout = devnull if verbosity == 0: stream = devnull ttr = unittest.TextTestRunner(verbosity=verbosity, stream=stream) origcwd = os.getcwd() testdir = tempfile.mkdtemp(prefix='ase-test-') os.chdir(testdir) if verbosity: print('{:25}{}\n'.format('test-dir', testdir), file=sys.__stdout__) try: results = ttr.run(ts) finally: os.chdir(origcwd) sys.stdout = sys.__stdout__ return results
def test(verbosity=1, calculators=[], testdir=None, stream=sys.stdout, files=None): test_calculator_names.extend(calculators) disable_calculators([name for name in calc_names if name not in calculators]) tests = get_tests(files) ts = unittest.TestSuite() for test in tests: ts.addTest(ScriptTestCase(filename=os.path.abspath(test))) if verbosity > 0: print_info() sys.stdout = devnull if verbosity == 0: stream = devnull ttr = unittest.TextTestRunner(verbosity=verbosity, stream=stream) origcwd = os.getcwd() if testdir is None: testdir = tempfile.mkdtemp(prefix='ase-test-') else: if os.path.isdir(testdir): shutil.rmtree(testdir) # clean before running tests! os.mkdir(testdir) os.chdir(testdir) if verbosity: print('test-dir ', testdir, '\n', file=sys.__stdout__) try: results = ttr.run(ts) finally: os.chdir(origcwd) sys.stdout = sys.__stdout__ return results
def run(args): if args.calculators: calculators = args.calculators.split(',') # Hack: We use ASE_TEST_CALCULATORS to communicate to pytest # (in conftest.py) which calculators we have enabled. # This also provides an (undocumented) way to enable # calculators when running pytest independently. os.environ['ASE_TEST_CALCULATORS'] = ' '.join(calculators) else: calculators = [] print_info() print() if args.list_calculators: for name in calc_names: print(name) sys.exit(0) for calculator in calculators: if calculator not in calc_names: sys.stderr.write('No calculator named "{}".\n' 'Possible CALCULATORS are: ' '{}.\n'.format(calculator, ', '.join(calc_names))) sys.exit(1) if args.nogui: os.environ.pop('DISPLAY') pytest_args = ['--pyargs', '-v'] def add_args(*args): pytest_args.extend(args) if args.list: add_args('--collect-only') jobs = choose_how_many_workers(args.jobs) if jobs: add_args('--numprocesses={}'.format(jobs)) add_args('--dist=loadfile') if args.fast: add_args('-m', 'not slow') if args.coverage: # It won't find the .coveragerc unless we are in the right # directory. cwd = Path.cwd() if testdir.parent.parent != cwd: raise CLIError('Please run ase test --coverage in the ase ' 'top directory') coveragerc = testdir / '.coveragerc' if not coveragerc.exists(): raise CLIError('No .coveragerc file. Maybe you are not ' 'running the development version. Please ' 'do so, or run coverage manually') add_args('--cov=ase', '--cov-config={}'.format(coveragerc), '--cov-report=term', '--cov-report=html') if args.tests: names, groups = all_test_modules_and_groups() testnames = [] for arg in args.tests: if arg in groups: testnames += groups[arg] else: testnames.append(arg) for testname in testnames: add_args('ase.test.{}'.format(testname)) else: add_args('ase.test') if args.verbose: add_args('--capture=no') if args.pytest: add_args(*args.pytest) print() calcstring = ','.join(calculators) if calculators else 'none' print('Enabled calculators: {}'.format(calcstring)) print() print('About to run pytest with these parameters:') for line in pytest_args: print(' ' + line) if not have_module('pytest'): raise CLIError( 'Cannot import pytest; please install pytest to run tests') import pytest exitcode = pytest.main(pytest_args) sys.exit(exitcode)
def run(args): if args.calculators: calculators = args.calculators.split(',') # Hack: We use ASE_TEST_CALCULATORS to communicate to pytest # (in conftest.py) which calculators we have enabled. # This also provides an (undocumented) way to enable # calculators when running pytest independently. os.environ['ASE_TEST_CALCULATORS'] = ' '.join(calculators) else: calculators = [] print_info() print() if args.list_calculators: for name in calc_names: print(name) sys.exit(0) for calculator in calculators: if calculator not in calc_names: sys.stderr.write('No calculator named "{}".\n' 'Possible CALCULATORS are: ' '{}.\n'.format(calculator, ', '.join(calc_names))) sys.exit(1) if args.nogui: os.environ.pop('DISPLAY') pytest_args = ['--pyargs', '-v'] def add_args(*args): pytest_args.extend(args) if args.list: add_args('--collect-only') jobs = choose_how_many_workers(args.jobs) if jobs: add_args('--numprocesses={}'.format(jobs)) if args.fast: add_args('-m', 'not slow') if args.coverage: add_args('--cov=ase', '--cov-config=.coveragerc', '--cov-report=term', '--cov-report=html') if args.tests: names, groups = all_test_modules_and_groups() testnames = [] for arg in args.tests: if arg in groups: testnames += groups[arg] else: testnames.append(arg) for testname in testnames: add_args('ase.test.{}'.format(testname)) else: add_args('ase.test') if args.verbose: add_args('--capture=no') if args.pytest: add_args(*args.pytest) print() calcstring = ','.join(calculators) if calculators else 'none' print('Enabled calculators: {}'.format(calcstring)) print() print('About to run pytest with these parameters:') for line in pytest_args: print(' ' + line) if not have_module('pytest'): raise CLIError('Cannot import pytest; please install pytest ' 'to run tests') # We run pytest through Popen rather than pytest.main(). # # This is because some ASE modules were already imported and # would interfere with code coverage measurement. # (Flush so we don't get our stream mixed with the pytest output) sys.stdout.flush() proc = Popen([sys.executable, '-m', 'pytest'] + pytest_args, cwd=str(testdir)) exitcode = proc.wait() sys.exit(exitcode)
def run(args): if args.calculators: calculators = args.calculators.split(',') # Hack: We use ASE_TEST_CALCULATORS to communicate to pytest # (in conftest.py) which calculators we have enabled. # This also provides an (undocumented) way to enable # calculators when running pytest independently. os.environ['ASE_TEST_CALCULATORS'] = ' '.join(calculators) else: calculators = [] print_info() print() if args.list_calculators: for name in calc_names: print(name) sys.exit(0) for calculator in calculators: if calculator not in calc_names: sys.stderr.write('No calculator named "{}".\n' 'Possible CALCULATORS are: ' '{}.\n'.format(calculator, ', '.join(calc_names))) sys.exit(1) if args.nogui: os.environ.pop('DISPLAY') pytest_args = ['--pyargs', '-v'] def add_args(*args): pytest_args.extend(args) if args.list: add_args('--collect-only') jobs = choose_how_many_workers(args.jobs) if jobs: add_args('--numprocesses={}'.format(jobs)) if args.tests: from ase.test.newtestsuite import TestModule dct = TestModule.all_test_modules_as_dict() # Hack: Make it recognize groups of tests like fio/*.py groups = {} for name in dct: groupname = name.split('.')[0] if groupname not in dct: groups.setdefault(groupname, []).append(name) testnames = [] for arg in args.tests: if arg in groups: testnames += groups[arg] else: testnames.append(arg) for testname in testnames: mod = dct[testname] if mod.is_pytest_style: pytest_args.append(mod.module) else: # XXX Not totally logical add_args('ase.test.test_modules::{}'.format( mod.pytest_function_name)) else: add_args('ase.test') if args.verbose: add_args('--capture=no') if args.pytest: add_args(*args.pytest) print() calcstring = ','.join(calculators) if calculators else 'none' print('Enabled calculators: {}'.format(calcstring)) print() print('About to run pytest with these parameters:') for line in pytest_args: print(' ' + line) if not have_module('pytest'): raise CLIError( 'Cannot import pytest; please install pytest to run tests') import pytest exitcode = pytest.main(pytest_args) sys.exit(exitcode)