简体   繁体   English

对于python鼻子测试类:传递可选参数的正确方法是什么

[英]For python nose test classes : What is the correct way to pass optional arguments

I have created a python class Test_getFileSize for use with nose 我已经创建了一个用于鼻子的python类Test_getFileSize

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. 如果我对包含此类的文件运行鼻子测试,它将使用默认值以及正确的setUp和tearDown方法正确测试该类。 Problem is that when I write a class to do just that, the setUp method never gets run. 问题是,当我编写一个类来执行此操作时,setUp方法永远不会运行。

What I want to be able to do is test different file sizes ( ie 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 谢谢吉姆

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. 您可以编写一个测试函数(不是类的一部分),其中测试函数本身是一个生成器,每个yield返回一个新函数,并与参数一起运行以生成另一个测试。 That would work well if you had 500 different filenames/filesizes as a list you wanted to test against. 如果您有500个不同的文件名/文件大小作为要测试的列表,那将很好地工作。

See here for a simple example/docs: http://nose.readthedocs.org/en/latest/writing_tests.html#test-generators 参见此处,了解一个简单的示例/文档: 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 鼻子,unittest.TestCase和元类:未发现自动生成的test_ *方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM