[Tutor] Writing to the file system and verify the files written out.

Becky Mcquilling ladymcse2000 at gmail.com
Fri Apr 15 08:33:52 CEST 2011


Hi, all:


I'm doing a tutorial online and I have run across an assignment to write a
unit test.  One of the tests is working fine, the other one is failing.


The goal of this particular one is:


1)  Create a directory

2)  Create files in the directory that are listed in the text files

3)  Create a set with the filenames of all the files that were created

3)  List the files files, using os.listdir

4)  Create a set with the above list

5)  Compare the two sets to ensure that no files were created that weren't
in the text files


The setUp() method and the tearDown() method work fine, as well as test_2(),
but test_1, is failing the unittest, with:


   FAIL: test_1 (setupDemo.FileTest)

Verify creation of files is possible

----------------------------------------------------------------------

Traceback (most recent call last):

  File "V:\workspace\Python2_Homework02\src\setupDemo.py", line 33, in
test_1

    self.assertEqual(dir_list, text_files, "The filelist is not equal")

AssertionError: The filelist is not equal


I'm not sure why the lists would not be equal


Here is the code:


"""

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"

        text_files = set()

        for filename in ("this.txt", "that.txt", "the_other.txt"):

            f = open(filename, "w")

            f.write("Some text\n")

            text_files.add(filename)

            f.close()

            self.assertTrue(f.closed)

        dir_list = os.listdir(self.dirname)

        dir_set = set()

        for file in dir_list:

            dir_set.add(file)

        self.assertEqual(dir_list, text_files, "The filelist is not equal")




    def test_2(self):

        "Verify that the current directory is empty"

        self.assertEqual(glob.glob("*"), [], "Directory not empty")



    def tearDown(self):

        os.chdir(self.origdir)

        shutil.rmtree(self.dirname)



if __name__ == "__main__":

    unittest.main()


Any help greatly appreciated!


Becky
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110414/15f4b379/attachment.html>


More information about the Tutor mailing list