Unittest - testing for filenames and filesize
Tigerstyle
laddosingh at gmail.com
Thu Aug 23 07:25:34 EDT 2012
Hi.
I need help with an assignment and I hope you guys can guide me in the right direction.
This is the code:
------------------
"""
Demostration of setUp and tearDown.
The tests do not actually test anything - this is a demo.
"""
import unittest
import tempfile
import shutil
import glob
import os
class FileTest(unittest.TestCase):
def setUp(self):
self.origdir = os.getcwd()
self.dirname = tempfile.mkdtemp("testdir")
os.chdir(self.dirname)
def test_1(self):
"Verify creation of files is possible"
for filename in ("this.txt", "that.txt", "the_other.txt"):
f = open(filename, "w")
f.write("Some text\n")
f.close()
self.assertTrue(f.closed)
def test_2(self):
"Verify that current directory is empty"
self.assertEqual(glob.glob("*"), [], "Directory not empty")
def tearDown(self):
os.chdir(self.origdir)
shutil.rmtree(self.dirname)
-------------
I need to modify this code as following:
1. The test_1() method includes code to verify that the test directory contains only the files created by the for loop. Hint: You might create a set containing the list of three filenames, and then create a set from the os.listdir() method.
2. A test_3() method creates a binary file that contains exactly a million bytes, closes it and then uses os.stat to verify that the file on disk is of the correct length (with os.stat, statinfo.st_size returns the size in bytes).
I'm new to Python programming so I don't know where to put the set in point 1. Before the test or under test1.
Would appreciate pointers and solutions (with explanation)for both point 1 and 2.
Thank you in advance.
T
More information about the Python-list
mailing list