我想为python MFCC功能提取器编写一些测试,以便使用nosetest运行.除了一些较低级别的测试,我还希望能够通过单元测试存储一些标准输入和预期输出文件.
目前我们正在对我们服务器上文件的路径进行硬编码,但我更希望测试文件(输入和预期输出)位于代码存储库中的某个位置,以便它们可以在测试的同时保持在源代码控制之下码.
我遇到的问题是我不确定放置测试文件的最佳位置,以及当nosetest调用每个测试函数时如何知道该路径是什么.目前我正在考虑将测试数据存储在与测试相同的文件夹中,并使用它__file__
来确定它的位置(会起作用吗?),但我愿意接受其他建议.
我已经在这个问题上待了一天了.:(无法搞清楚.请帮助.我有这个设置:
class Parent(object):传递#common stuff到这里
A类(Parent,unittest.TestCase):在这里传递#A东西
class B(Parent,unittest.TestCase):在这里传递#B的东西
我想使用nosetests只运行B类和父类的测试,因为B继承自父类.我以为我会使用-m或--match来放置类名,但它似乎不起作用.
默认匹配是使用--match是'(?:^ | [\ b _./-])[Tt]\test'
有任何想法吗?
提前致谢!
我试图找出是否有办法让鼻子测试跑步者运行所有测试,除了具有特定标签的测试.看起来这可以通过属性实现,但我不知道是否有办法用标签来做,标签是属性的一个子集.
目前我通过调用使用标签
nosetests -a tags='tag'
Run Code Online (Sandbox Code Playgroud)
我的标记测试看起来像:
@attr(tags=['foo', 'bar', 'baz'])
def test_some_stuff(self):
Run Code Online (Sandbox Code Playgroud)
但如果我想运行除'baz'标记之外的所有测试,我该怎么做?
我试过像
nosetests -A 'not baz'
nosetests -a '!baz'
nosetests tags='!baz'
Run Code Online (Sandbox Code Playgroud)
但是这些似乎对属性没有任何影响.我没有看到在文档中排除标记的示例:http://nose.readthedocs.org/en/latest/plugins/attrib.html
我宁愿不必添加一个新标签,只是为了排除我正在使用的这个大型测试套件,并且必须记住总是添加一个假的"排除标签".
我刚开始一个python项目,我正在尝试不同的测试框架.我遇到的问题是nose2找不到我的测试:
$ nose2 --verbose
在0.000秒内进行0测试
好
而鼻子测试找到了所有
$ nosetests - 仅收集
.................................
跑了33个测试在0.004s
好
其他我可以在同一目录下使用nose2执行单个测试:
$ nose2 myproj.client.test.mypkg.mymodule_test
.
以0.007s进行1次测试
好
myproj.client.test.mypkg.mymodule_test的位置如下:
'''
Created on 18/04/2013
@author: julia
'''
from unittest import TestCase, main
import os
from myproj.client.mymodule import SUT
from mock import Mock
import tempfile
class SUTTest(TestCase):
def setUp(self):
self.folder = tempfile.mkdtemp(suffix='myproj')
self.sut = SUT(self.folder, Mock())
self.sut.init()
def test_wsName(self):
myfolder = os.path.join(self.folder, 'myfolder')
os.mkdir(myfolder)
self.sut.change_dir(myfolder)
self.assertEquals(self.SUT.name, 'myfolder')
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
我一直在看文档,我找不到可能的原因.
在MacOs 10.8.3上运行python 2.7.3
在鼻子测试中,我知道您可以通过鼻子测试配置文件指定要运行的测试,如下所示:
[nosetests]
tests=testIWT_AVW.py:testIWT_AVW.tst_bynd1,testIWT_AVW.py:testIWT_AVW.tst_bynd3
Run Code Online (Sandbox Code Playgroud)
然而,当添加大量测试时,上面的内容看起来很混乱并且变得更难维护,尤其是在无法使用换行符的情况下。我发现能够使用 unittests TestSuite 功能指定要运行的测试要方便得多。例如
def custom_suite():
suite = unittest.TestSuite()
suite.addTest(testIWT_AVW('tst_bynd1'))
suite.addTest(testIWT_AVW('tst_bynd3'))
return suite
if __name__=="__main__":
runner = unittest.TextTestRunner()
runner.run(custom_suite())
Run Code Online (Sandbox Code Playgroud)
问题:如何在我的 .py 文件中指定应该由鼻子测试运行哪些测试?
谢谢。
PS如果有一种方法可以通过nosetest配置文件指定测试,并且不会强制将所有测试都写在一行上,我也会对它开放,作为第二种选择
我创建了以下鼻子测试:
@nottest
def _test_validate_helper_eq(self, input, expected, *args, **kwargs):
result = testedObcject.validatePrice(input, *args, **kwargs)
eq_(result, expected)
def test_validate_price(self):
yield self._test_validate_helper_eq, {}, {'price':'0'}
yield self._test_validate_helper_eq, {}, {'price', -1}, 'price', -1`
Run Code Online (Sandbox Code Playgroud)
validatePrice是测试对象的函数:
def validatePrice(self, input, name = 'price', default_price=0):
...
return validated_input
Run Code Online (Sandbox Code Playgroud)
所以我有一个测试生成器,使用_test_validate_helper_eq
函数生成2个测试.还_test_validate_helper_eq
函数采用不同数量的参数,并将其传递至validatePrice
首先测试PASS,但第二个测试有问题.正如你所看到的,我正在传递额外的2个参数,name
并且default_price
.此测试失败,并出现以下错误:AssertionError: {'price': u'-1'} != set(['price', -1])
事实证明,expected
参数的值是一个set
而不是我定义的字典.我不知道这种转换是由鼻子还是因为*args
并且**kwargs
被使用.
任何想法是什么以及如何解决它?
I just run into a problem with my testsuite. I am using a setup with nosetests, SQLAlchemy, Flask and Factory-Boy
I have the following code:
def _create_fixtures(self):
self.user = UserFactory()
pprint(db.query(User).all())
db.add(self.user)
pprint(db.query(User).all())
Run Code Online (Sandbox Code Playgroud)
witch returns following:
[<User #1>, <User #2>]
[<User #1>, <User #2>]
Run Code Online (Sandbox Code Playgroud)
My UserFactory looks like this:
class UserFactory(Factory):
FACTORY_FOR = User
FACTORY_SESSION = db
email = Sequence(lambda n: 'user{0}@wohnortfinder.com'.format(n))
password = "password"
username = Sequence(lambda n: 'user{0}'.format(n))
Run Code Online (Sandbox Code Playgroud)
(yes I am using the normal Factory and not he …
在我正在运行的一系列测试中nosetests
,assertEqual(a,b)
失败,a
并且b
(相当长的字符串)被逐字打印填充屏幕,混淆了其他所有内容.您可以通过将其添加到您的一个测试用例来创建类似的情况:
def test_my_long_strings(self):
self.assertEqual('a'*5000, 'b'*5000)
Run Code Online (Sandbox Code Playgroud)
我试着设置--verbosity=0
和--debug-log=File
,但他们都没有任何影响,这两个字符串仍印在屏幕上.
无论如何都要关闭assertEqual
详细程度或将其重定向到除stderr之外的单独文件(还报告测试失败/通过)?
我正在尝试使用nose_parameterized
测试并希望将其用于单元测试方法。
from nose.tools import assert_equal
from nose_parameterized import parameterized
import unittest
Class TestFoo(unittest.TestCase):
def setUp(self):
self.user1 = "Bar"
self.user2 = "Foo"
@parameterized.expand([
("testuser1",self.user1,"Bar"),
("testuser2",self.user2,"Foo")
]
def test_param(self,name,input,expected):
assert_equal(input,expected)
Run Code Online (Sandbox Code Playgroud)
但self
在装饰器函数中没有定义。有解决方法吗?我知道我可以使用全局类变量,但我需要在setUp
.
python nose nosetests parameterized-unit-test nose-parameterized
我正在尝试设置Python来执行鼻子,但只在我正在本地开发的现有应用程序上.我不希望鼻子在当前安装的所有库中运行.但是,我希望鼻子能够发现当前工作目录和子目录中的任何测试.
首先,我要做的就是确保我正在使用的参数被使用(由下面的@ need-batchelder解决).但是,目前看来我正在传递的参数被忽略,并且正在进行测试的全局发现(即从python文件夹中获取测试).
来自文档:
-V, --version
Output nose version and exit
Run Code Online (Sandbox Code Playgroud)
从命令行运行nosetests -V会产生预期的版本输出:
nosetests -V
nosetests-script.py version 1.2.1
Run Code Online (Sandbox Code Playgroud)
但是,以下测试脚本开始运行它可以找到的每个测试,包括安装在python路径中的库,而不是当前工作目录的一部分,即使它位于应用程序的根目录中:
import nose, os
def main():
print os.getcwd()
x=raw_input() #This is just so I can see the output of the cwd before it launches into testing everything it can find.
result = nose.run(argv=['-V'])
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
谢谢
编辑:尝试@ ned-batchelder的建议允许我使用给定的参数运行鼻子,但不允许在应用程序文件夹中发现测试.所以,如果我这样做,我可以传递参数,但我无法测试我的应用程序.
好的,问题是:在Zed Shaw的"学习Python艰难之路" 练习49中,我们需要测试几个例外assert_raises()
.这是我正在测试的函数,如果省略该assert_raises()
部分,它会通过测试:
def parse_verb(word_list):
skip(word_list, 'stop')
if peek(word_list) == 'verb':
return match(word_list, 'verb')
else:
raise ParserError("Expected a verb next.")
Run Code Online (Sandbox Code Playgroud)
这是测试功能和臭名昭着的assert_raises()
线:
def parse_verb_test():
vrb_list = [('of', 'stop'), ('from', 'stop'), ('go', 'verb'), ('north', 'direction')]
assert_equal(parse_verb(vrb_list), ('go', 'verb'))
assert_equal(vrb_list, [('north', 'direction')])
assert_raises(ParserError, parse_verb, vrb_list)
Run Code Online (Sandbox Code Playgroud)
它给了我以下输出:
.........E...
======================================================================
ERROR: tests.parser_tests.parse_verb_test
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/vsevolod/Repositories/ex48/tests/parser_tests.py", line 20, in parse_verb_test
assert_raises(ParserError, parse_verb, vrb_list)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 471, …
Run Code Online (Sandbox Code Playgroud) 这是我的 Flask-Restful 应用程序中的 report_runner.py 中的 ReportRunner 类中的方法:
class ReportRunner(object):
def __init__(self):
pass
def setup_routes(self, app):
app.add_url_rule("/run_report", view_func=self.run_report)
def request_report(self, key):
# code #
def key_exists(self, key):
# code #
def run_report(self):
key = request.args.get("key", "")
if self.key_exists(key):
self.request_report(report_type, key)
return jsonify(message = "Success! Your report has been created.")
else:
response = jsonify({"message": "Error => report key not found on server."})
response.status_code = 404
return response
Run Code Online (Sandbox Code Playgroud)
鼻子测试调用与该路由关联的 URL
def setUp(self):
self.setup_flask()
self.controller = Controller()
self.report_runner = ReportRunner()
self.setup_route(self.report_runner)
def test_run_report(self):
rr …
Run Code Online (Sandbox Code Playgroud) 我的nosetests套装中有一个通用测试类和一些继承自它的子类。
配置同样是:
class CGeneral_Test(object)::
"""This class defines the general testcase"""
def __init__ (self):
do_some_init()
print "initialisation of general class done!"
def setUp(self):
print "this is the general setup method"
do_setup()
def tearDown(self):
print "this is the general teardown method"
do_teardown()
Run Code Online (Sandbox Code Playgroud)
现在,我的子类如下所示:
class CIPv6_Test(CGeneral_Test):
"""This class defines the sub, inherited testcase"""
def __init__ (self):
super(CIPv6_Test, self).__init__()
do_some_sub_init()
print "initialisation of sub class done!"
def setUp(self):
print "this is the per-test sub setup method"
do_sub_setup()
def test_routing_64(self):
do_actual_testing_scenarios()
def tearDown(self):
print "this …
Run Code Online (Sandbox Code Playgroud) nosetests ×13
python ×10
nose ×3
python-2.7 ×3
assert ×1
dictionary ×1
factory-boy ×1
flask ×1
json ×1
set ×1
sqlalchemy ×1
tags ×1
unit-testing ×1