def test1b_msg_reporting_utility(self): g, e = "got this", "expected that" m, p = "your message here", "pfx" tmsg0 = m + "\n .....got:'" + g + \ "'\n expected:'" + e +"'" tmsg1 = p + ": " + tmsg0 self.assertEqual(tu.msg(g, e, m), tmsg0, "non-prefix message") self.assertEqual(tu.msg(g, e, m, p), tmsg1, "prefix message")
def test1b_msg_reporting_utility(self): g,e = "got this", "expected that" m,p = "your message here", "pfx" tmsg0 = m + "\n .....got:'" + g + \ "'\n expected:'" + e +"'" tmsg1 = p + ": " + tmsg0 self.assertEqual(tu.msg(g,e,m), tmsg0, "non-prefix message") self.assertEqual(tu.msg(g,e,m,p), tmsg1, "prefix message")
def test2c_path_append_parent(self): here = tu.absdir(); par = os.path.dirname(here) was_there = par in sys.path if was_there: while par in sys.path: sys.path.remove(par) np = len(sys.path) for p in (None, __file__): self.assertFalse(par in sys.path, "par not in initial path") if not p: g = tu.path_append_parent() else: g = tu.path_append_parent(p) self.assertEqual(g,par, tu.msg(g,par, "path_append_parent return")) self.assertTrue(par in sys.path, "actually appends") sys.path.remove(par) l= len(sys.path) self.assertEqual(l, np, tu.msg(l, np,"numpaths")) if was_there: # restore entry state (but no multiples needed!) sys.path.append(par)
def test2c_path_append_parent(self): here = tu.absdir() par = os.path.dirname(here) was_there = par in sys.path if was_there: while par in sys.path: sys.path.remove(par) np = len(sys.path) for p in (None, __file__): self.assertFalse(par in sys.path, "par not in initial path") if not p: g = tu.path_append_parent() else: g = tu.path_append_parent(p) self.assertEqual(g, par, tu.msg(g, par, "path_append_parent return")) self.assertTrue(par in sys.path, "actually appends") sys.path.remove(par) l = len(sys.path) self.assertEqual(l, np, tu.msg(l, np, "numpaths")) if was_there: # restore entry state (but no multiples needed!) sys.path.append(par)
def test4a(self): wmsg = "a warning message" emsg = "an error message" import logging # file logging helps with file capture of log-messages tl = tu.TestLogger() for i in (1,2): # 2 passes to test clearing old file tl.logfile_init(self.logf) logging.warn(wmsg) logging.info("nada") logging.error(emsg) ll = tl.logfile_getlines() nl = len(ll) self.assertEquals(nl,3, tu.msg(nl,3, "pass %d: expected line count" % i))
def test4a(self): wmsg = "a warning message" emsg = "an error message" import logging # file logging helps with file capture of log-messages tl = tu.TestLogger() for i in (1, 2): # 2 passes to test clearing old file tl.logfile_init(self.logf) logging.warn(wmsg) logging.info("nada") logging.error(emsg) ll = tl.logfile_getlines() nl = len(ll) self.assertEquals( nl, 3, tu.msg(nl, 3, "pass %d: expected line count" % i))
def test3a_subdir(self): for sub in self.asubs: self.assertFalse(os.path.isdir(sub), "init: no dir %r" % sub) b,d = os.path.dirname(sub), os.path.basename(sub) md = tu.make_subdir(d, b) self.assertTrue(os.path.isdir(sub), "made dir %r" % sub) self.assertEqual(md,sub, tu.msg(md,sub, "make_subdir returns path")) s2 = os.path.join(sub,"sub2") tu.make_subdir("sub2", sub) self.assertTrue(os.path.isdir(s2), "made dir %r" % s2) f = os.path.join(s2,"test_file") open(f,"w").write("testing..") self.assertTrue(os.path.isfile(f), "file %r exists" % f) tu.delete_tree(sub) self.assertFalse(os.path.isdir(sub), "delete_tree removes subdir %r" % sub )
def test3a_subdir(self): for sub in self.asubs: self.assertFalse(os.path.isdir(sub), "init: no dir %r" % sub) b, d = os.path.dirname(sub), os.path.basename(sub) md = tu.make_subdir(d, b) self.assertTrue(os.path.isdir(sub), "made dir %r" % sub) self.assertEqual(md, sub, tu.msg(md, sub, "make_subdir returns path")) s2 = os.path.join(sub, "sub2") tu.make_subdir("sub2", sub) self.assertTrue(os.path.isdir(s2), "made dir %r" % s2) f = os.path.join(s2, "test_file") open(f, "w").write("testing..") self.assertTrue(os.path.isfile(f), "file %r exists" % f) tu.delete_tree(sub) self.assertFalse(os.path.isdir(sub), "delete_tree removes subdir %r" % sub)
def test2b_absdir(self): here = tu.absdir() g = tu.absdir(__file__) self.assertEqual(g, here, tu.msg(g, here, "absdir"))
def test2a_context_via_traceback(self): e = os.path.basename(__file__).rstrip(".co") # eg in *.py[co] g = os.path.basename(tu._caller_context()[0]).rstrip('co') self.assertEqual(g, e, tu.msg(g, e, "_caller_context"))
def test2a_context_via_traceback(self): e = os.path.basename(__file__).rstrip(".co") # eg in *.py[co] g = os.path.basename(tu._caller_context()[0]).rstrip('co') self.assertEqual(g,e, tu.msg(g,e, "_caller_context"))
def test2b_absdir(self): here = tu.absdir(); g=tu.absdir(__file__) self.assertEqual(g, here, tu.msg(g, here, "absdir"))