Unittest - testing for filenames and filesize

Discussion in 'Python' started by Tigerstyle, Aug 23, 2012.

  1. Tigerstyle

    Tigerstyle Guest

    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
    Tigerstyle, Aug 23, 2012
    #1
    1. Advertising

  2. Tigerstyle

    Roy Smith Guest

    In article <>,
    Tigerstyle <> wrote:

    > Hi.
    >
    > I need help with an assignment and I hope you guys can guide me in the right
    > direction.
    > [code elided]
    > 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.


    I'm not sure what your question is. The hint you give above pretty much
    tells you what to do. The basic issue here is that you started out with
    a list (well, tuple) of filenames. You can use os.listdir() to get a
    list of filenames that exist in the current directory. The problem is
    that you can't compare these two lists directly, because lists are
    ordered. Converting both lists to sets eliminates the ordering and lets
    you compare them.

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


    I think you want to end up with something like:

    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")
    f.close()
    self.assertTrue(f.closed)
    dir_names = os.listdir()
    self.assertEqual(set(dir_names), set(filenames))

    The above code isn't tested, but it should give you the gist of what you
    need to do.
    Roy Smith, Aug 23, 2012
    #2
    1. Advertising

  3. Tigerstyle

    Terry Reedy Guest

    On 8/23/2012 8:28 AM, Roy Smith wrote:

    > I think you want to end up with something like:
    >
    > 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")
    > f.close()
    > self.assertTrue(f.closed)
    > dir_names = os.listdir()
    > self.assertEqual(set(dir_names), set(filenames))
    >
    > The above code isn't tested, but it should give you the gist of what you
    > need to do.


    One can start with a set rather than tuple of file names.

    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")
    f.close()
    self.assertTrue(f.closed)
    dir_names = set(os.listdir())
    self.assertEqual(dir_names, filenames)

    --
    Terry Jan Reedy
    Terry Reedy, Aug 23, 2012
    #3
  4. Tigerstyle

    Roy Smith Guest

    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.
    Roy Smith, Aug 23, 2012
    #4
  5. Tigerstyle

    Roy Smith Guest

    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.
    Roy Smith, Aug 23, 2012
    #5
  6. Tigerstyle

    Tigerstyle Guest

    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")
    os.chdir(self.dirname)

    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")
    f.close()
    self.assertTrue(f.closed)
    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
    f.write(filesize)
    f.close()
    self.assertEqual(os.stat, filesize)
    def tearDown(self):
    os.chdir(self.origdir)
    shutil.rmtree(self.dirname

    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?

    Thanks

    T

    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 onlyavailable in (IIRC), 2.7 or above.
    Tigerstyle, Aug 24, 2012
    #6
  7. Tigerstyle

    Tigerstyle Guest

    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")
    os.chdir(self.dirname)

    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")
    f.close()
    self.assertTrue(f.closed)
    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
    f.write(filesize)
    f.close()
    self.assertEqual(os.stat, filesize)
    def tearDown(self):
    os.chdir(self.origdir)
    shutil.rmtree(self.dirname

    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?

    Thanks

    T

    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 onlyavailable in (IIRC), 2.7 or above.
    Tigerstyle, Aug 24, 2012
    #7
  8. Tigerstyle

    Robert Day Guest

    On Fri, 2012-08-24 at 09:20 -0700, Tigerstyle wrote:

    > def test_3(self):
    > f = open("test.dat", "wb")
    > filesize = b"0"*1000000
    > f.write(filesize)
    > f.close()
    > self.assertEqual(os.stat, filesize)


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


    rob@rivertam:~$ python
    Python 2.7.3 (default, Jul 24 2012, 10:05:38)
    [GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.stat

    <built-in function stat>
    >>>


    So that's what 'os.stat' is. Why are you testing whether that's equal to
    b"0"*1000000?

    (You may find the documentation on os.stat at
    http://docs.python.org/library/os.html#os.stat helpful; it's a function
    which takes a path as its argument, and returns an object with some
    relevant attributes.)
    Robert Day, Aug 24, 2012
    #8
  9. Tigerstyle

    Tigerstyle Guest

    Thanks Rob,

    I'v modified the test_3 like this:


    def test_3(self):
    f = open("test.dat", "wb")
    filesize = (b'b'*1000000)
    f.write(filesize)
    f.close()
    statinfo = os.stat("test.dat")
    self.assertEqual(statinfo.st_size, filesize)

    I'm still getting AssertionError and the error says: 1000000 !=b'

    Help appreciated.

    T

    kl. 21:04:54 UTC+2 fredag 24. august 2012 skrev Robert Day følgende:
    > On Fri, 2012-08-24 at 09:20 -0700, Tigerstyle wrote:
    >
    >
    >
    > > def test_3(self):

    >
    > > f = open("test.dat", "wb")

    >
    > > filesize = b"0"*1000000

    >
    > > f.write(filesize)

    >
    > > f.close()

    >
    > > self.assertEqual(os.stat, filesize)

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

    >
    > >

    >
    >
    >
    > rob@rivertam:~$ python
    >
    > Python 2.7.3 (default, Jul 24 2012, 10:05:38)
    >
    > [GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
    >
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    > >>> import os

    >
    > >>> os.stat

    >
    > <built-in function stat>
    >
    > >>>

    >
    >
    >
    > So that's what 'os.stat' is. Why are you testing whether that's equal to
    >
    > b"0"*1000000?
    >
    >
    >
    > (You may find the documentation on os.stat at
    >
    > http://docs.python.org/library/os.html#os.stat helpful; it's a function
    >
    > which takes a path as its argument, and returns an object with some
    >
    > relevant attributes.)
    Tigerstyle, Aug 26, 2012
    #9
  10. Tigerstyle

    Tigerstyle Guest

    Thanks Rob,

    I'v modified the test_3 like this:


    def test_3(self):
    f = open("test.dat", "wb")
    filesize = (b'b'*1000000)
    f.write(filesize)
    f.close()
    statinfo = os.stat("test.dat")
    self.assertEqual(statinfo.st_size, filesize)

    I'm still getting AssertionError and the error says: 1000000 !=b'

    Help appreciated.

    T

    kl. 21:04:54 UTC+2 fredag 24. august 2012 skrev Robert Day følgende:
    > On Fri, 2012-08-24 at 09:20 -0700, Tigerstyle wrote:
    >
    >
    >
    > > def test_3(self):

    >
    > > f = open("test.dat", "wb")

    >
    > > filesize = b"0"*1000000

    >
    > > f.write(filesize)

    >
    > > f.close()

    >
    > > self.assertEqual(os.stat, filesize)

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

    >
    > >

    >
    >
    >
    > rob@rivertam:~$ python
    >
    > Python 2.7.3 (default, Jul 24 2012, 10:05:38)
    >
    > [GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
    >
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    > >>> import os

    >
    > >>> os.stat

    >
    > <built-in function stat>
    >
    > >>>

    >
    >
    >
    > So that's what 'os.stat' is. Why are you testing whether that's equal to
    >
    > b"0"*1000000?
    >
    >
    >
    > (You may find the documentation on os.stat at
    >
    > http://docs.python.org/library/os.html#os.stat helpful; it's a function
    >
    > which takes a path as its argument, and returns an object with some
    >
    > relevant attributes.)
    Tigerstyle, Aug 26, 2012
    #10
  11. Tigerstyle

    Rob Day Guest

    On Sun, 2012-08-26 at 10:36 -0700, Tigerstyle wrote:
    > self.assertEqual(statinfo.st_size, filesize)
    >
    > I'm still getting AssertionError and the error says: 1000000 !=b'
    >
    >


    filesize is the character 'b' repeated one million times (the contents
    of the file, in other words). statinfo.st_size is the number of bytes in
    the file, i.e. 1,000,000. So when your assertEqual code checks if those
    two values are equal, what do you think happens?
    Rob Day, Aug 26, 2012
    #11
  12. Tigerstyle

    Tigerstyle Guest

    Ahhhhhh,

    thank you very much Rob.

    Fixed now.

    Have a great day.

    T

    kl. 19:51:54 UTC+2 søndag 26. august 2012 skrev Rob Day følgende:
    > On Sun, 2012-08-26 at 10:36 -0700, Tigerstyle wrote:
    >
    > > self.assertEqual(statinfo.st_size, filesize)

    >
    > >

    >
    > > I'm still getting AssertionError and the error says: 1000000 !=b'

    >
    > >

    >
    > >

    >
    >
    >
    > filesize is the character 'b' repeated one million times (the contents
    >
    > of the file, in other words). statinfo.st_size is the number of bytes in
    >
    > the file, i.e. 1,000,000. So when your assertEqual code checks if those
    >
    > two values are equal, what do you think happens?
    Tigerstyle, Aug 26, 2012
    #12
  13. Tigerstyle

    Tigerstyle Guest

    Ahhhhhh,

    thank you very much Rob.

    Fixed now.

    Have a great day.

    T

    kl. 19:51:54 UTC+2 søndag 26. august 2012 skrev Rob Day følgende:
    > On Sun, 2012-08-26 at 10:36 -0700, Tigerstyle wrote:
    >
    > > self.assertEqual(statinfo.st_size, filesize)

    >
    > >

    >
    > > I'm still getting AssertionError and the error says: 1000000 !=b'

    >
    > >

    >
    > >

    >
    >
    >
    > filesize is the character 'b' repeated one million times (the contents
    >
    > of the file, in other words). statinfo.st_size is the number of bytes in
    >
    > the file, i.e. 1,000,000. So when your assertEqual code checks if those
    >
    > two values are equal, what do you think happens?
    Tigerstyle, Aug 26, 2012
    #13
  14. 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
    Chris Withers, Aug 31, 2012
    #14
  15. On Saturday, September 1, 2012 12:19:10 AM UTC+8, Chris Withers wrote:
    > 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


    Well, I am thinking that the directory tree listing services or daemons
    supported by the OS by some iterators could be better than the stack
    based model.
    88888 Dihedral, Sep 1, 2012
    #15
  16. On Saturday, September 1, 2012 12:19:10 AM UTC+8, Chris Withers wrote:
    > 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


    Well, I am thinking that the directory tree listing services or daemons
    supported by the OS by some iterators could be better than the stack
    based model.
    88888 Dihedral, Sep 1, 2012
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    725
    Toby Inkster
    Apr 23, 2005
  2. Paul Moore
    Replies:
    9
    Views:
    590
    Roy Smith
    Feb 21, 2005
  3. Paul  Moore
    Replies:
    1
    Views:
    379
    Paul Moore
    Oct 14, 2008
  4. C.S.M.G.Sarma
    Replies:
    6
    Views:
    806
    Ben Bacarisse
    Jul 15, 2010
  5. Tim Chase
    Replies:
    8
    Views:
    228
    Roy Smith
    Sep 29, 2011
Loading...

Share This Page