查看PyDev的文档及其在Pydev站点的测试集成,屏幕截图显示了每个测试的输出以及测试运行所需的时间.但是当我运行它时,输出中只显示失败的测试,即使这样,它所用的时间也是空的.我试图在pyunit testrunner配置中增加详细程度,但这只是给系统输出窗口提供了更多输出,而不是PyUnit窗口.有人知道怎么修这个东西吗?(这是4/4 PyDev 2.0版本)
我正在使用nosetest工具来断言python unittest:
...
from nose.tools import assert_equals, assert_almost_equal
class TestPolycircles(unittest.TestCase):
def setUp(self):
self.latitude = 32.074322
self.longitude = 34.792081
self.radius_meters = 100
self.number_of_vertices = 36
self.vertices = polycircles.circle(latitude=self.latitude,
longitude=self.longitude,
radius=self.radius_meters,
number_of_vertices=self.number_of_vertices)
def test_number_of_vertices(self):
"""Asserts that the number of vertices in the approximation polygon
matches the input."""
assert_equals(len(self.vertices), self.number_of_vertices)
...
Run Code Online (Sandbox Code Playgroud)
当我跑步时python setup.py test,我得到一个弃用警告:
...
Asserts that the number of vertices in the approximation polygon ...
/Users/adamatan/personal/polycircles/polycircles/test/test_polycircles.py:22:
DeprecationWarning: Please use assertEqual instead.
assert_equals(len(self.vertices), self.number_of_vertices)
ok
...
Run Code Online (Sandbox Code Playgroud)
我assertEqual在鼻子工具中找不到任何东西.这个警告来自哪里,我该如何解决?
我有一个我想确认的生成器已经结束(在程序的某一点.我在python 2.7中使用unittest
# it is a generator whould have only one item
item = it.next()
# any further next() calls should raise StopIteration
self.assertRaises(StopIteration, it.next())
Run Code Online (Sandbox Code Playgroud)
但它失败了
======================================================================
ERROR: test_productspider_parse_method (__main__.TestMyMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/myName/tests/__main__.py", line 94, in test_my_method
self.assertRaises(StopIteration, it.next())
StopIteration
----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud) 我的Python版本是3.5.1
我有一个简单的代码(tests.py):
import unittest
class SimpleObject(object):
array = []
class SimpleTestCase(unittest.TestCase):
def test_first(self):
simple_object = SimpleObject()
simple_object.array.append(1)
self.assertEqual(len(simple_object.array), 1)
def test_second(self):
simple_object = SimpleObject()
simple_object.array.append(1)
self.assertEqual(len(simple_object.array), 1)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
如果我用命令'python tests.py'运行它,我会得到结果:
.F
======================================================================
FAIL: test_second (__main__.SimpleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 105, in test_second
self.assertEqual(len(simple_object.array), 1)
AssertionError: 2 != 1
----------------------------------------------------------------------
Ran 2 tests in 0.003s
FAILED (failures=1)
Run Code Online (Sandbox Code Playgroud)
为什么会这样?以及如何解决它.我希望每个测试运行都是独立的(每个测试都应该通过),但它并不像我们所看到的那样.
进行单元测试时,我希望能够进行设置和访问flask.g。
flask.g['test'] = {'test': '123'}
test_dict = flask.g['test']
Run Code Online (Sandbox Code Playgroud)
产生此错误:
Error
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 346, in __setitem__
self._get_current_object()[key] = value
TypeError: '_AppCtxGlobals' object does not support item assignment
Run Code Online (Sandbox Code Playgroud)
但是,当我在生产环境中运行它时,一切正常。如果我使用setattr和getattr,则单元测试有效,但在生产中中断了,则出现以下错误
Error
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs) …Run Code Online (Sandbox Code Playgroud) 包装结构:
pqr/pq.py
test.py
Run Code Online (Sandbox Code Playgroud)
pqr/pq.py 有以下结构
其中lmn是全局安装的pip模块
结构 pq.py
from lmn import Lm
class Ab():
def __init__(self):
self.lm = Lm()
def echo(self, msg):
print msg
Run Code Online (Sandbox Code Playgroud)
test.py 有以下结构
from pqr.pq import Ab
Run Code Online (Sandbox Code Playgroud)
如何Lm()在这里模拟类以便测试Ab类中的所有方法?
这是在Python 2.7。我有一个名为的类class A,并且有一些属性由用户设置时会引发异常:
myA = A()
myA.myattribute = 9 # this should throw an error
Run Code Online (Sandbox Code Playgroud)
我想写一个unittest确保它引发错误的代码。
创建测试类并继承后unittest.TestCase,我尝试编写如下测试:
myA = A()
self.assertRaises(AttributeError, eval('myA.myattribute = 9'))
Run Code Online (Sandbox Code Playgroud)
但是,这引发了syntax error。但是,如果尝试eval('myA.myattribute = 9'),它将抛出属性错误,应有的错误。
如何编写单元测试以正确测试?
谢谢。
后:
import unittest
loader = unittest.TestLoader()
tests = loader.discover('.')
testRunner = unittest.runner.TextTestRunner()
testResult = testRunner.run(tests)
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式获取列表失败名称和消息:
for t in testResult.failures:
print t[0].id()
print t[1]
Run Code Online (Sandbox Code Playgroud)
如何为成功做同样的事情?
我希望能够通过重写中的内容来做到这一点TextTestRunner。最简单的方法是什么?
在Python 2.7上测试。
我正在使用python项目unittest进行测试以及coverage代码覆盖.
我广泛使用接口模式,我注意到整体代码覆盖率很大程度上受"未经测试的接口"的影响.
考虑以下:
class IReader(object):
@abstractmethod
def read(self):
pass
class Reader(IReader):
def read(self):
# whatever
Run Code Online (Sandbox Code Playgroud)
我测试Reader但(显然)我没有测试,IReader因此pass指令被标记为未被测试覆盖.
有没有办法忽略接口coverage?
由于这是我的第一个python项目之一,我这样做完全错了吗?
考虑这个测试
import shutil, tempfile
from os import path
import unittest
from pathlib import Path
class TestExample(unittest.TestCase):
def setUp(self):
# Create a temporary directory
self.test_dir = tempfile.TemporaryDirectory()
self.test_dir2 = tempfile.mkdtemp()
def tearDown(self):
# Remove the directory after the test
shutil.rmtree(self.test_dir2)
shutil.rmtree(self.test_dir.name) #throws error
def test_something(self):
self.assertTrue(Path(self.test_dir.name).is_dir())
self.assertTrue(Path(self.test_dir2).is_dir())
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
在tearDown但引发错误
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpxz7ts7a7'
Run Code Online (Sandbox Code Playgroud)
指的是self.test_dir.name.
根据源代码tempfile,两个元素是相同的.
def __init__(self, suffix=None, prefix=None, dir=None):
self.name = mkdtemp(suffix, prefix, dir) …Run Code Online (Sandbox Code Playgroud) python-unittest ×10
python ×9
unit-testing ×5
python-2.7 ×4
python-3.x ×2
deprecated ×1
flask ×1
mocking ×1
namespaces ×1
nose ×1
pydev ×1
python-3.3 ×1
shutil ×1