Add import_function method to test.test_support, and modify a number of

tests that expect to be skipped if imports fail or functions don't
exist to use import_function and import_module.  The ultimate goal is
to change regrtest to not skip automatically on ImportError.  Checking
in now to make sure the buldbots don't show any errors on platforms
I can't direct test on.
This commit is contained in:
R. David Murray 2009-03-30 19:04:00 +00:00
parent bb8cb0e192
commit 59beec326a
20 changed files with 78 additions and 41 deletions

View file

@ -1,11 +1,12 @@
# Copyright (C) 2003 Python Software Foundation # Copyright (C) 2003 Python Software Foundation
import unittest import unittest
import aepack
import aetypes
import os import os
from test import test_support from test import test_support
aetypes = test_support.import_module('aetypes')
aepack = test_support.import_module('aepack')
class TestAepack(unittest.TestCase): class TestAepack(unittest.TestCase):
OBJECTS = [ OBJECTS = [
aetypes.Enum('enum'), aetypes.Enum('enum'),

View file

@ -1,13 +1,15 @@
# Copyright (C) 2003 Python Software Foundation # Copyright (C) 2003 Python Software Foundation
import unittest import unittest
import macostools
import Carbon.File
import MacOS
import os import os
from test import test_support from test import test_support
import struct import struct
MacOS = test_support.import_module('MacOS')
# The following should exist if MacOS does.
import macostools
import applesingle import applesingle
import Carbon.File
AS_MAGIC=0x00051600 AS_MAGIC=0x00051600
AS_VERSION=0x00020000 AS_VERSION=0x00020000

View file

@ -1,11 +1,13 @@
# test asynchat -- requires threading # test asynchat
import thread # If this fails, we can't test this module
import asyncore, asynchat, socket, threading, time import asyncore, asynchat, socket, threading, time
import unittest import unittest
import sys import sys
from test import test_support from test import test_support
# Skip tests if thread module does not exist.
test_support.import_module('thread')
HOST = test_support.HOST HOST = test_support.HOST
SERVER_QUIT = 'QUIT\n' SERVER_QUIT = 'QUIT\n'

View file

@ -2,8 +2,9 @@
"""Whimpy test script for the cd module """Whimpy test script for the cd module
Roger E. Masse Roger E. Masse
""" """
import cd from test.test_support import verbose, import_module
from test.test_support import verbose
cd = import_module('cd')
cdattrs = ['BLOCKSIZE', 'CDROM', 'DATASIZE', 'ERROR', 'NODISC', 'PAUSED', 'PLAYING', 'READY', cdattrs = ['BLOCKSIZE', 'CDROM', 'DATASIZE', 'ERROR', 'NODISC', 'PAUSED', 'PLAYING', 'READY',
'STILL', '__doc__', '__name__', 'atime', 'audio', 'catalog', 'control', 'createparser', 'error', 'STILL', '__doc__', '__name__', 'atime', 'audio', 'catalog', 'control', 'createparser', 'error',

View file

@ -2,8 +2,9 @@
"""Whimpy test script for the cl module """Whimpy test script for the cl module
Roger E. Masse Roger E. Masse
""" """
import cl from test.test_support import verbose, import_module
from test.test_support import verbose
cl = import_module('cl')
clattrs = ['ADDED_ALGORITHM_ERROR', 'ALAW', 'ALGORITHM_ID', clattrs = ['ADDED_ALGORITHM_ERROR', 'ALAW', 'ALGORITHM_ID',
'ALGORITHM_VERSION', 'AUDIO', 'AWARE_ERROR', 'AWARE_MPEG_AUDIO', 'ALGORITHM_VERSION', 'AUDIO', 'AWARE_ERROR', 'AWARE_MPEG_AUDIO',

View file

@ -1,6 +1,7 @@
from test import test_support from test import test_support
import unittest import unittest
import crypt
crypt = test_support.import_module('crypt')
class CryptTestCase(unittest.TestCase): class CryptTestCase(unittest.TestCase):

View file

@ -3,14 +3,12 @@
import os import os
import time import time
import unittest
from test.fork_wait import ForkWait from test.fork_wait import ForkWait
from test.test_support import run_unittest, reap_children from test.test_support import run_unittest, reap_children, import_function
#Skip test if fork does not exist.
import_function(os, 'fork')
try:
os.fork
except AttributeError:
raise unittest.SkipTest, "os.fork not defined -- skipping test_fork1"
class ForkTest(ForkWait): class ForkTest(ForkWait):
def wait_impl(self, cpid): def wait_impl(self, cpid):

View file

@ -3,8 +3,10 @@
taken mostly from the documentation. taken mostly from the documentation.
Roger E. Masse Roger E. Masse
""" """
from test.test_support import verbose from test.test_support import verbose, import_module
import gl, GL, time import time
gl = import_module('gl')
GL = import_module('GL')
glattrs = ['RGBcolor', 'RGBcursor', 'RGBmode', 'RGBrange', 'RGBwritemask', glattrs = ['RGBcolor', 'RGBcursor', 'RGBmode', 'RGBrange', 'RGBwritemask',
'__doc__', '__name__', 'addtopup', 'altgetmatrix', 'arc', 'arcf', '__doc__', '__name__', 'addtopup', 'altgetmatrix', 'arc', 'arcf',

View file

@ -1,9 +1,10 @@
"""Test script for the grp module.""" """Test script for the grp module."""
import grp
import unittest import unittest
from test import test_support from test import test_support
grp = test_support.import_module('grp')
class GroupDatabaseTestCase(unittest.TestCase): class GroupDatabaseTestCase(unittest.TestCase):
def check_value(self, value): def check_value(self, value):

View file

@ -1,10 +1,12 @@
import unittest import unittest
import MacOS
import Carbon.File
from test import test_support from test import test_support
import os import os
import subprocess import subprocess
MacOS = test_support.import_module('MacOS')
#The following should exist if MacOS exists.
import Carbon.File
TESTFN2 = test_support.TESTFN + '2' TESTFN2 = test_support.TESTFN + '2'
class TestMacOS(unittest.TestCase): class TestMacOS(unittest.TestCase):

View file

@ -1,13 +1,15 @@
# Copyright (C) 2003 Python Software Foundation # Copyright (C) 2003 Python Software Foundation
import unittest import unittest
import macostools
import Carbon.File
import MacOS
import os import os
import sys import sys
from test import test_support from test import test_support
MacOS = test_support.import_module('MacOS')
#The following modules should exist if MacOS exists.
import Carbon.File
import macostools
TESTFN2 = test_support.TESTFN + '2' TESTFN2 = test_support.TESTFN + '2'
class TestMacostools(unittest.TestCase): class TestMacostools(unittest.TestCase):

View file

@ -1,8 +1,9 @@
from test.test_support import TESTFN, run_unittest from test.test_support import TESTFN, run_unittest, import_module
import mmap
import unittest import unittest
import os, re import os, re
mmap = import_module('mmap')
PAGESIZE = mmap.PAGESIZE PAGESIZE = mmap.PAGESIZE
class MmapTests(unittest.TestCase): class MmapTests(unittest.TestCase):

View file

@ -2,10 +2,6 @@
from test import test_support from test import test_support
try:
import posix
except ImportError:
raise unittest.SkipTest, "posix is not available"
import time import time
import os import os
@ -13,6 +9,9 @@ import pwd
import shutil import shutil
import unittest import unittest
import warnings import warnings
posix = test_support.import_module('posix')
warnings.filterwarnings('ignore', '.* potential security risk .*', warnings.filterwarnings('ignore', '.* potential security risk .*',
RuntimeWarning) RuntimeWarning)

View file

@ -1,7 +1,7 @@
import unittest import unittest
from test import test_support from test import test_support
import pwd pwd = test_support.import_module('pwd')
class PwdTest(unittest.TestCase): class PwdTest(unittest.TestCase):

View file

@ -2,7 +2,8 @@
import unittest import unittest
from test import test_support from test import test_support
import aetools
aetools = test_support.import_module('aetools')
class TestScriptpackages(unittest.TestCase): class TestScriptpackages(unittest.TestCase):

View file

@ -9,9 +9,11 @@
import unittest import unittest
from test import test_support from test import test_support
import os
from os import path
startfile = test_support.import_function(os, 'startfile')
# use this form so that the test is skipped when startfile is not available:
from os import startfile, path
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
def test_nonexisting(self): def test_nonexisting(self):

View file

@ -55,6 +55,20 @@ def import_module(name, deprecated=False):
else: else:
return module return module
def import_function(module, name, deprecated=False):
with warnings.catch_warnings():
if deprecated:
warnings.filterwarnings("ignore", ".+ (module|package)",
DeprecationWarning)
try:
function = getattr(module, name)
except AttributeError:
raise unittest.SkipTest("No function named %s in module %s" % (
name, module.__name__))
else:
return function
verbose = 1 # Flag set to 0 by regrtest.py verbose = 1 # Flag set to 0 by regrtest.py
use_resources = None # Flag set to [] by regrtest.py use_resources = None # Flag set to [] by regrtest.py
max_memuse = 0 # Disable bigmem tests (they will still be run with max_memuse = 0 # Disable bigmem tests (they will still be run with

View file

@ -1,10 +1,12 @@
import os import os
import sys import sys
import ttk
import unittest import unittest
from _tkinter import TclError
from test import test_support from test import test_support
ttk = test_support.import_module('ttk')
#If ttk exists _tkinter must exist.
from _tkinter import TclError
try: try:
ttk.Button() ttk.Button()
except TclError, msg: except TclError, msg:

View file

@ -1,12 +1,15 @@
# Test the windows specific win32reg module. # Test the windows specific win32reg module.
# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
from _winreg import *
import os, sys import os, sys
import unittest import unittest
from test import test_support from test import test_support
#Do this first so test will be skipped if module doesn't exist
test_support.import_module('_winreg')
#Now import everything
from _winreg import *
test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me" test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
test_data = [ test_data = [

View file

@ -2,10 +2,12 @@
import unittest import unittest
from test import test_support from test import test_support
import winsound, time import time
import os import os
import subprocess import subprocess
winsound = test_support.import_module('winsound')
class BeepTest(unittest.TestCase): class BeepTest(unittest.TestCase):
# As with PlaySoundTest, incorporate the _have_soundcard() check # As with PlaySoundTest, incorporate the _have_soundcard() check