Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unittest - testing for filenames and filesize

Reply
Thread Tools

Unittest - testing for filenames and filesize

 
 
Tigerstyle
Guest
Posts: n/a
 
      08-23-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
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      08-23-2012
In article <6b0299df-bc24-406b-8d69->,
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.
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      08-23-2012
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

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      08-23-2012
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.
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      08-23-2012
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.
 
Reply With Quote
 
Tigerstyle
Guest
Posts: n/a
 
      08-24-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")
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.

 
Reply With Quote
 
Tigerstyle
Guest
Posts: n/a
 
      08-24-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")
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.

 
Reply With Quote
 
Robert Day
Guest
Posts: n/a
 
      08-24-2012
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:3
[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.)

 
Reply With Quote
 
Tigerstyle
Guest
Posts: n/a
 
      08-26-2012
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:3
>
> [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.)

 
Reply With Quote
 
Tigerstyle
Guest
Posts: n/a
 
      08-26-2012
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:3
>
> [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.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unittest testing assert*() calls rather than methods? Tim Chase Python 8 09-29-2011 03:20 AM
Initialized and Uninitialized Global Variables and Executable FileSize C.S.M.G.Sarma C Programming 6 07-15-2010 03:06 PM
Unittest - adding a doctest suite to unittest.main Paul Moore Python 1 10-14-2008 03:32 PM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM
Unittest - testing properties (read-only attributes) Paul Moore Python 9 02-21-2005 05:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57