Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27724 > unrolled thread
| Started by | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| First post | 2012-08-23 04:25 -0700 |
| Last post | 2012-08-31 22:08 -0700 |
| Articles | 16 — 7 participants |
Back to article view | Back to comp.lang.python
Unittest - testing for filenames and filesize Tigerstyle <laddosingh@gmail.com> - 2012-08-23 04:25 -0700
Re: Unittest - testing for filenames and filesize Roy Smith <roy@panix.com> - 2012-08-23 08:28 -0400
Re: Unittest - testing for filenames and filesize Terry Reedy <tjreedy@udel.edu> - 2012-08-23 13:29 -0400
Re: Unittest - testing for filenames and filesize Roy Smith <roy@panix.com> - 2012-08-23 12:06 -0700
Re: Unittest - testing for filenames and filesize Roy Smith <roy@panix.com> - 2012-08-23 12:06 -0700
Re: Unittest - testing for filenames and filesize Tigerstyle <laddosingh@gmail.com> - 2012-08-24 09:20 -0700
Re: Unittest - testing for filenames and filesize Tigerstyle <laddosingh@gmail.com> - 2012-08-24 09:20 -0700
Re: Unittest - testing for filenames and filesize Robert Day <robertkday@gmail.com> - 2012-08-24 20:04 +0100
Re: Unittest - testing for filenames and filesize Tigerstyle <laddosingh@gmail.com> - 2012-08-26 10:36 -0700
Re: Unittest - testing for filenames and filesize Rob Day <rkd@rkd.me.uk> - 2012-08-26 18:51 +0100
Re: Unittest - testing for filenames and filesize Tigerstyle <laddosingh@gmail.com> - 2012-08-26 11:37 -0700
Re: Unittest - testing for filenames and filesize Tigerstyle <laddosingh@gmail.com> - 2012-08-26 11:37 -0700
Re: Unittest - testing for filenames and filesize Tigerstyle <laddosingh@gmail.com> - 2012-08-26 10:36 -0700
Re: Unittest - testing for filenames and filesize Chris Withers <chris@python.org> - 2012-08-31 17:19 +0100
Re: Unittest - testing for filenames and filesize 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-31 22:08 -0700
Re: Unittest - testing for filenames and filesize 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-31 22:08 -0700
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2012-08-23 04:25 -0700 |
| Subject | Unittest - testing for filenames and filesize |
| Message-ID | <6b0299df-bc24-406b-8d69-489e990d8e4f@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-08-23 08:28 -0400 |
| Message-ID | <roy-CDABA6.08285523082012@news.panix.com> |
| In reply to | #27724 |
In article <6b0299df-bc24-406b-8d69-489e990d8e4f@googlegroups.com>,
Tigerstyle <laddosingh@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-08-23 13:29 -0400 |
| Message-ID | <mailman.3724.1345742998.4697.python-list@python.org> |
| In reply to | #27728 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-08-23 12:06 -0700 |
| Message-ID | <37ff0f1f-d36c-4573-8179-2d27ea404f37@googlegroups.com> |
| In reply to | #27750 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-08-23 12:06 -0700 |
| Message-ID | <mailman.3728.1345748791.4697.python-list@python.org> |
| In reply to | #27750 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2012-08-24 09:20 -0700 |
| Message-ID | <mailman.3760.1345825232.4697.python-list@python.org> |
| In reply to | #27760 |
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 only available in (IIRC), 2.7 or above.
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2012-08-24 09:20 -0700 |
| Message-ID | <8447c547-8472-48c6-ad78-87989c1ce288@googlegroups.com> |
| In reply to | #27760 |
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 only available in (IIRC), 2.7 or above.
[toc] | [prev] | [next] | [standalone]
| From | Robert Day <robertkday@gmail.com> |
|---|---|
| Date | 2012-08-24 20:04 +0100 |
| Message-ID | <mailman.3766.1345835099.4697.python-list@python.org> |
| In reply to | #27811 |
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.)
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2012-08-26 10:36 -0700 |
| Message-ID | <d22938d4-2404-4fca-8e2c-04a11cb3a6c3@googlegroups.com> |
| In reply to | #27821 |
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.)
[toc] | [prev] | [next] | [standalone]
| From | Rob Day <rkd@rkd.me.uk> |
|---|---|
| Date | 2012-08-26 18:51 +0100 |
| Message-ID | <mailman.3847.1346003518.4697.python-list@python.org> |
| In reply to | #27936 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2012-08-26 11:37 -0700 |
| Message-ID | <eec83184-3b53-425e-a0cf-6b103b14ee1c@googlegroups.com> |
| In reply to | #27938 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2012-08-26 11:37 -0700 |
| Message-ID | <mailman.3848.1346006223.4697.python-list@python.org> |
| In reply to | #27938 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2012-08-26 10:36 -0700 |
| Message-ID | <mailman.3846.1346002565.4697.python-list@python.org> |
| In reply to | #27821 |
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.)
[toc] | [prev] | [next] | [standalone]
| From | Chris Withers <chris@python.org> |
|---|---|
| Date | 2012-08-31 17:19 +0100 |
| Message-ID | <mailman.6.1346431828.27098.python-list@python.org> |
| In reply to | #27724 |
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
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-08-31 22:08 -0700 |
| Message-ID | <eb40fee2-685d-45e6-9ce2-04269fa80970@googlegroups.com> |
| In reply to | #28186 |
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.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-08-31 22:08 -0700 |
| Message-ID | <mailman.23.1346476139.27098.python-list@python.org> |
| In reply to | #28186 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web