Unittest - testing for filenames and filesize

T

Tigerstyle

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
 
R

Roy Smith

Tigerstyle said:
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.
 
T

Terry Reedy

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

Roy Smith

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

Roy Smith

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

Tigerstyle

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:
 
T

Tigerstyle

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:
 
R

Robert Day

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

Tigerstyle

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:
def test_3(self):
f = open("test.dat", "wb")
filesize = b"0"*1000000


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.



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

Tigerstyle

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:
def test_3(self):
f = open("test.dat", "wb")
filesize = b"0"*1000000


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.



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

Rob Day

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

Tigerstyle

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:
 
T

Tigerstyle

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:
 
C

Chris Withers

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
 
8

88888 Dihedral

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



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

88888 Dihedral

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



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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top