Unittest - testing for filenames and filesize

Tigerstyle laddosingh at gmail.com
Fri Aug 24 18:20:28 CEST 2012

Thank you guys, Roy and Terry.

I has been great help.

I still need some help. Here is the updated 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")
    def test_1(self):
        "Verify creation of files is possible"
        filenames = {"this.txt", "that.txt", "the_other.txt"} 
        for filename in filenames: 
            f = open(filename, "w")
            f.write("Some text\n")
        dir_names = set(os.listdir('.')) 
        self.assertEqual(set(dir_names), set(filenames)) 
    def test_2(self):
        "Verify that current directory is empty"
        self.assertEqual(glob.glob("*"), [], "Directory not empty")
    def test_3(self):
        f = open("test.dat", "wb")
        filesize = b"0"*1000000
        self.assertEqual(os.stat, filesize)
    def tearDown(self):

The test_3 is to test if the created binary file har the size of 1 million bytes. Somehow it is not working. Any suggestions?


kl. 21:06:29 UTC+2 torsdag 23. august 2012 skrev Roy Smith følgende:
> On Thursday, August 23, 2012 1:29:19 PM UTC-4, Terry Reedy wrote:
> > One can start with a set rather than tuple of file names.
> >          filenames = {"this.txt", "that.txt", "the_other.txt"}
> Yeah, that's even cleaner.  Just be aware, the set notation above is only available in (IIRC), 2.7 or above.

More information about the Python-list mailing list