1import os 2import sys 3import unittest 4from cStringIO import StringIO 5from optparse import OptionParser 6import nose.core 7from nose.config import Config, all_config_files 8from nose.tools import set_trace 9from mock import Bucket, MockOptParser 10 11 12class NullLoader: 13 def loadTestsFromNames(self, names): 14 return unittest.TestSuite() 15 16class TestAPI_run(unittest.TestCase): 17 18 def test_restore_stdout(self): 19 print "AHOY" 20 s = StringIO() 21 print s 22 stdout = sys.stdout 23 conf = Config(stream=s) 24 # set_trace() 25 print "About to run" 26 res = nose.core.run( 27 testLoader=NullLoader(), argv=['test_run'], env={}, config=conf) 28 print "Done running" 29 stdout_after = sys.stdout 30 self.assertEqual(stdout, stdout_after) 31 32class Undefined(object): 33 pass 34 35class TestUsage(unittest.TestCase): 36 37 def test_from_directory(self): 38 usage_txt = nose.core.TestProgram.usage() 39 assert usage_txt.startswith('nose collects tests automatically'), ( 40 "Unexpected usage: '%s...'" % usage_txt[0:50].replace("\n", '\n')) 41 42 def test_from_zip(self): 43 requested_data = [] 44 45 # simulates importing nose from a zip archive 46 # with a zipimport.zipimporter instance 47 class fake_zipimporter(object): 48 49 def get_data(self, path): 50 requested_data.append(path) 51 # Return as str in Python 2, bytes in Python 3. 52 return '<usage>'.encode('utf-8') 53 54 existing_loader = getattr(nose, '__loader__', Undefined) 55 try: 56 nose.__loader__ = fake_zipimporter() 57 usage_txt = nose.core.TestProgram.usage() 58 self.assertEqual(usage_txt, '<usage>') 59 self.assertEqual(requested_data, [os.path.join( 60 os.path.dirname(nose.__file__), 'usage.txt')]) 61 finally: 62 if existing_loader is not Undefined: 63 nose.__loader__ = existing_loader 64 else: 65 del nose.__loader__ 66 67 68class DummyTestProgram(nose.core.TestProgram): 69 def __init__(self, *args, **kwargs): 70 pass 71 72 73class TestProgramConfigs(unittest.TestCase): 74 75 def setUp(self): 76 self.program = DummyTestProgram() 77 78 def test_getAllConfigFiles(self): 79 self.assertEqual(self.program.getAllConfigFiles(), all_config_files()) 80 81 def test_getAllConfigFiles_ignore_configs(self): 82 env = {'NOSE_IGNORE_CONFIG_FILES': 'yes'} 83 self.assertEqual(self.program.getAllConfigFiles(env), []) 84 85 def test_makeConfig(self): 86 calls = [] 87 class TestProgramMock(DummyTestProgram): 88 def getAllConfigFiles(self, env): 89 calls.append(env) 90 return [] 91 92 program = TestProgramMock() 93 env = {'foo': 'bar'} 94 program.makeConfig(env) 95 self.assertEqual(calls, [env]) 96 97 98if __name__ == '__main__': 99 unittest.main() 100