0

I have created a python class Test_getFileSize for use with nose

relevant sections:

def __init__(self,mytestfile="./filetest",testsize=102400):
    ''' Constructor'''
    print " Running __init__", testsize,mytestfile
    self.testsize=testsize
    self.mytestfile = mytestfile

and the workhorse method:

@with_setup(setUp, tearDown)
def test_getFileSize(self):
     from nose.tools import ok_, eq_,with_setup
     import mp4
     with open(self.mytestfile,"rb") as out:
          filesize=mp4.getFileSize(out)
          eq_(self.testsize,filesize,msg='Passed Test  size')
          print "Results ", filesize,self.testsize

If I run nosetest against the file containing this class, it correctly tests the class using the default values and the correct setUp and tearDown methods. Problem is that when I write a class to do just that, the setUp method never gets run.

What I want to be able to do is test different file sizes ( i.e. pass a filesize value).

If there is a better way to do it, I am all ears. I would prefer not to do it via the command line if possible.

Thanks Jim

Paulo Almeida
  • 7,803
  • 28
  • 36

1 Answers1

0

You could write a test function (not part of a class) where the test function itself is a generator, with each yield returning a new function to run with arguments to generate another test. That would work well if you had 500 different filenames/filesizes as a list you wanted to test against.

See here for a simple example/docs: http://nose.readthedocs.org/en/latest/writing_tests.html#test-generators

With a test class, things get a bit trickier - since it doesn't allow you to use this generator method for class methods. You could use a metaclass to return a class with a suitable number of functions to run your test (one per case, for example.) but that might be beyond what you want to do.

That being said, you might find it sufficient to have a single test method that iterates over a list of filenames/sizes and performs the test on each one. The work there is significantly less, but also results in a single "test" output line for the collective set of tests.

You might reference this question for an answer as to how one person did this:

nose, unittest.TestCase and metaclass: auto-generated test_* methods not discovered

Community
  • 1
  • 1
chander
  • 1,997
  • 2
  • 16
  • 15
  • Hello I investigated your method and found that I have an issue with it: I was using the class structure as a way of passing arguments ( in this case filename and size) to setup. I lose that functionality switching over to just functions. Is there a cleaner option that using global ? – Jim Ramberg Aug 08 '13 at 20:09
  • You could use a metaclass to generate new methods based on the iterator - assuming you know in advance of creating an instance of the test class what you wanted to test. – chander Aug 09 '13 at 13:07
  • 1
    Never used metaclass before. Still a bit of a paduwan with respect to my python. Could you recommend a good resource on it ? thanks – Jim Ramberg Aug 09 '13 at 14:10