Added 7 new test cases to test_os.py which tests the os module.
Following is a test class to test os.renames(). It starts by creating a temporary directory tree, and then renaming it. A walk is performed on the top most directory later on, and it is examined to verify that the renaming has taken place.
class RenamesDirTest(unittest.TestCase):
def setUp(self):
if os.path.exists(support.TESTFN):
os.remove(support.TESTFN)
os.mkdir(support.TESTFN)
def test_renames(self):
base=support.TESTFN
self.old=os.path.join(base, "dir1", "dir2", "dir3", "dir4")
os.makedirs(self.old)
self.new=os.path.join(base, "dir1", "dir2", "test3", "test4")
os.renames(self.old, self.new)
for (dirpath, dirnames, filenames) in os.walk(base, topdown=False):
self.assertEqual(self.new, dirpath)
break
def tearDown(self):
if os.path.exists(self.new):
os.removedirs(self.new)
elif os.path.exists(self.old):
os.removedirs(self.old)
A test for os.chdir(). Current directory for the current process is changed using os.chdir(), and later confirmed by calling os.getcwd(). A test case to see that os.chdir() fails when called with a directory argument that doesn't exist is also added.
#Tests for changing directory paths
class ChangePathTests(unittest.TestCase):
def setUp(self):
self.tempdir=support.TESTFN
os.mkdir(self.tempdir)
self.tempdir2=support.TESTFN+"2"
def test_chdir(self):
if os.path.exists(self.tempdir):
os.chdir(self.tempdir)
cwd=os.getcwd()
self.assertEqual(cwd, self.tempdir)
#Test to check chdir fails if nonexisting directory passed
def test_chdir_nonexistent(self):
if os.path.exists(self.tempdir2):
os.rmdir(self.tempdir2)
try:
os.chdir(self.tempdir2)
except OSError:
pass
else:
self.fail("Did not raise OSError")
def tearDown(self):
if os.path.exists(self.tempdir):
os.rmdir(self.tempdir)
A test class to test os.geteuid() and os.getgid(). Both are verified by comparing with values returned by os.stat().
class PosixGetUidGidTests(unittest.TestCase):
def setUp(self):
f=os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
os.close(f)
self.stats=os.stat(__file__)
def tearDown(self):
if os.path.exists(support.TESTFN):
os.remove(support.TESTFN)
if hasattr(os, "geteuid"):
def test_geteuid(self):
self.assertEqual(os.geteuid(), self.stats.st_uid)
if hasattr(os, "getgid"):
def test_getgid(self):
self.assertEqual(os.getgid(), self.stats.st_gid)
A test class for testing os.getenv() and os.putenv().
class GetPutEnvironTests(unittest.TestCase):
def test_putenv(self):
try:
os.putenv("KEY", "VALUE")
except:
self.fail("Not able to set environment variable")
def test_getenv(self):
keyvalue={"KEY":"VALUE"}
os.environ["KEY"]=keyvalue["KEY"]
value=os.getenv("KEY")
self.assertEqual(value, keyvalue["KEY"])
I am designing a few more tests to test the os.spawn* family of functions.
0 Comments:
Subscribe to:
Post Comments (Atom)