我正在尝试使用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
我有一些功能仅在特定的物理位置运行,并且通过主机名来识别。这是在调用 socket.gethostname() 的 cython 模块中生成的。
无论如何,使用 socket.gethostname() 进行测试是否有与运行测试的主机不同的数据?
我是新来的unittest
。我试图根据列表中的值跳过测试用例。
class UESpecTest(unittest.TestCase):
global testcases_exec_list
testcases = []
testcases = testcases_exec_list
@unittest.skipIf('0' not in self.testcases, "Testcase input not given")
def test_retrieve_spec_info(self):
read_spec_info.open_csv(self.spec_info)
assert (bool(self.spec_info) == True) #Raise assertion if dictionary is empty
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
File "test_ue_cap_main.py", line 39, in UESpecTest
@unittest.skipIf('0' not in self.testcases, "Testcase input not given")
NameError: name 'self' is not defined
Run Code Online (Sandbox Code Playgroud)
我不确定为什么 self 在这里未定义。
在我的项目上运行buildout操作后,我可以nose
使用以下命令运行:
# ./bin/nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.310s
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试传递选项时(例如-w
对于基本目录,我得到以下内容:
# ./bin/nosetests -vv --detailed-errors --exe
Usage: nosetests [options]
nosetests: error: no such option: -v
Run Code Online (Sandbox Code Playgroud)
我已经检查了正在运行的测试文件,并删除了所有导入的行getopt
或OptionParser
确保它们没有妨碍,但我仍然得到相同的错误,无论如何.
我相信我们正在测试的其中一个文件需要getopt
运行...有没有办法nosetests
在没有这些错误的情况下使用buildout?
我正在做一些测试.一堆我的测试函数有共同的设置,所以我决定我应该使用@with_setup
decorator nose.tools
.我把问题简化为:
from nose.tools import with_setup
class TestFooClass(unittest.TestCase):
def setup_foo_value(self):
self.foo = 'foobar'
@with_setup(setup_foo_value)
def test_something(self):
print self.foo
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
$ python manage.py test tests/test_baz.py
E
======================================================================
ERROR: test_something (project.tests.test_baz.TestFooClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/user/Coding/project-backend/project/../project/tests/test_baz.py", line 17, in test_something
print self.foo
AttributeError: 'TestFooClass' object has no attribute 'foo'
----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
这就像setup_foo_value
没有被运行一样.任何帮助将非常感激!
在我的背景下,我无法将头包裹在Bamboo周围.我有功能测试,它们与正在构建的实际产品分开存放.我们正在尝试使用Bamboo来签出和构建代码,运行单元/集成测试以及所有传递是否都运行功能测试.这里的要求是功能测试在远程客户端上运行,该客户端与检出/构建代码并运行所有单元/集成测试的服务器通信.我安装了此服务器的远程代理,以通过在远程代理中设置命令功能来运行功能测试.问题是我需要从不同的工作目录开始调用我的测试.一旦调用了测试,我需要Bamboo来消耗这些结果.
我正在尝试设置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的建议允许我使用给定的参数运行鼻子,但不允许在应用程序文件夹中发现测试.所以,如果我这样做,我可以传递参数,但我无法测试我的应用程序.
我正在使用鼻子运行一堆测试用例.我想将每个案例的输出记录到单独的文件中,并且知道每个案例的结果[成功/失败].不幸的是,我无法弄清楚如何用鼻子做到这一点.任何人都可以提供一些线索吗?谢谢
仅当我运行单元测试时,是什么导致我的 django 应用程序出现此错误?为什么它认为nose.util.C
是一个模型?
RuntimeError:模型类nose.util.C 未声明显式app_label,并且不在INSTALLED_APPS 中的应用程序中,或者在加载其应用程序之前导入。
我有一个相当基本的问题.我正在nosetests
为我的python应用程序的测试套件运行命令.我想放入一个交互式调试器.当测试运行时,它会撞到我的IPython.embed()
线并冻结,没有提示.Ctrl + C将其杀死并恢复测试.
如何在运行nosetests时进入某种交互式提示符?
谢谢你的帮助.
import unittest
class TestTemplate(unittest.TestCase):
@classmethod
def setUpClass(self):
self.result = 'error'
print "setUpClass"
@classmethod
def tearDownClass(self):
print "The value of result is, ",self.result
if self.result == 'ok':
print "it is working"
print "The value of cls result is : ", self.result
print "TearDownClass"
class MyTest(TestTemplate):
def test_method_one(self):
self.result = 'ok'
print self.result
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
在tearDownClass
self.result 的值是error
,而是应该是okay
因为我在方法中改变了它?这有什么解决办法吗?