Unittest - testing for filenames and filesize

Chris Withers chris at python.org
Fri Aug 31 12:19:10 EDT 2012


On 23/08/2012 12:25, Tigerstyle wrote:
> class FileTest(unittest.TestCase):
>
>      def setUp(self):
>          self.origdir = os.getcwd()
>          self.dirname = tempfile.mkdtemp("testdir")
>          os.chdir(self.dirname)

I wouldn't change directories like this, it's pretty fragile, just use 
absolute paths.

>      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)

Seeing this, you might find the following tools useful:

http://packages.python.org/testfixtures/files.html

cheers,

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
             - http://www.simplistix.co.uk



More information about the Python-list mailing list