有人可以告诉我如何为使用nose创建的sqlalchemy模型编写单元测试.
我只需要一个简单的例子.
谢谢.
我想我想做的事情很简单.我想在测试设置函数中初始化几个变量,然后在用该设置修饰的测试函数中使用它们.以下简单的例子说明了我的意思:
from nose.tools import *
def setup():
foo = 10
def teardown():
foo = None
@with_setup(setup, teardown)
def test_foo_value():
assert_equal(foo, 10)
Run Code Online (Sandbox Code Playgroud)
这导致:
$ nosetests tests/test_foo.py
E
======================================================================
ERROR: test_foo.test_foo_value
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mtozzi/.virtualenvs/foo/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/mtozzi/code/foo/tests/test_foo.py", line 12, in test_foo_value
assert_equal(foo, 10)
NameError: global name 'foo' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.006s
FAILED (errors=1)
Run Code Online (Sandbox Code Playgroud)
使用旧的unittest样式,我可以将它们设置为测试类的实例变量,但我认为nosetests不要求你使用类.我也考虑将它们设置为包全局变量,但这似乎不是一个非常好的做法.我希望有一些显而易见的事情我不知道这样做.
在此先感谢您的帮助.
我已经使用以下命令来运行测试并评估Python项目的代码覆盖率超过一年了.
nosetests -v --with-coverage --cover-package=genhub genhub/*.py
Run Code Online (Sandbox Code Playgroud)
覆盖率报告过去包括最右边的一列,显示缺少覆盖范围的行.
Name Stmts Miss Branch BrPart Cover Missing
----------------------------------------------------------------
genhub/cdhit.py 50 0 8 0 100%
genhub/exons.py 85 69 8 0 17% 24-40, 48-56, 60-79, 87-107, 129-132, 138-141, 147-150
genhub/fasta.py 76 0 26 0 100%
genhub/genomedb.py 205 153 48 0 21% 40-43, 53-60, 64-65, 70, 74, 82, 86, 90, 98-99, 103-104, 108-109, 113-114, 118-119, 123-124, 128-129, 143-144, 152-154, 158-160, 164-166, 175, 180, 240-280, 289, 292, 295, 308-317, 323-330, 351-377, 380-386, 396-413, 419-430, 436-443, 449-456
genhub/iloci.py …Run Code Online (Sandbox Code Playgroud) 我有两个文件
spike.py
class T1(object):
def foo(self, afd):
return "foo"
def get_foo(self):
return self.foo(1)
def bar():
return "bar"
Run Code Online (Sandbox Code Playgroud)
test_spike.py:
from unittest import TestCase
import unittest
from mock import patch, MagicMock
from spike import T1, bar
class TestStuff(TestCase):
@patch('spike.T1.foo', MagicMock(return_value='patched'))
def test_foo(self):
foo = T1().get_foo()
self.assertEqual('patched', foo)
@patch('spike.bar')
def test_bar(self, mock_obj):
mock_obj.return_value = 'patched'
bar = bar()
self.assertEqual('patched', bar)
if __name__ == "__main__":
unittest.main()
Run Code Online (Sandbox Code Playgroud)
当我运行时python test_spike.py,第一个测试用例会通过,但第二个测试用例会失败.然后我切换到使用nosetests test_spike.py,然后两个都失败了.
我不明白这是怎么发生的?这些案件应该通过所有.
我们有CentOS和古老的Python 2.4解释器.
但我们想用更新的2.5/2.6语法编写测试.
假设我们安装了第二个Python解释器(例如python2.6)有没有办法运行'nosetests'hell命令并告诉它使用特定的python解释器而不是默认的?
是否可以在自定义类中运行nose测试生成器?我试图将示例转换为基于类的简单版本:
file: trial.py
>>>>>>>>>>>>>>
class ATest():
def test_evens(self):
for i in range(0, 5):
yield self.check_even, i, i * 3
def check_even(self, n, nn):
assert n % 2 == 0 or nn % 2 == 0
Run Code Online (Sandbox Code Playgroud)
这导致了
$ nosetests -v trial.py
----------------------------------------------------------------------
Ran 0 tests in 0.000s
Run Code Online (Sandbox Code Playgroud)
我查看了更改日志,并认为这应该从版本0.9.0a1开始工作.
我哪里错了?
我一直在接受
没有发现任何测试
Pycharm中的错误,我无法弄清楚为什么我会得到它...这就是我对我的看法point_test.py:
import unittest
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
from ..point import Point
class TestPoint(unittest.TestCase):
def setUp(self):
pass
def xyCheck(self,x,y):
point = Point(x,y)
self.assertEqual(x,point.x)
self.assertEqual(y,point.y)
Run Code Online (Sandbox Code Playgroud)
而这point.py,我正在尝试测试:
import unittest
from .utils import check_coincident, shift_point
class Point(object):
def __init__(self,x,y,mark={}):
self.x = x
self.y = y
self.mark = mark
def patched_coincident(self,point2):
point1 = (self.x,self.y)
return check_coincident(point1,point2)
def patched_shift(self,x_shift,y_shift):
point = (self.x,self.y)
self.x,self,y = shift_point(point,x_shift,y_shift)
Run Code Online (Sandbox Code Playgroud)
我的运行配置有问题吗?我看了这篇 SO帖子,但我仍然感到困惑.我的运行配置目前看起来像这样:
我想我只是明白我可能做错了什么?任何帮助将不胜感激,谢谢!
我的SUT看起来像:
foo.py
bar.py
tests/__init__.py [empty]
tests/foo_tests.py
tests/bar_tests.py
tests/integration/__init__.py [empty]
tests/integration/foo_tests.py
tests/integration/bar_tests.py
Run Code Online (Sandbox Code Playgroud)
当我跑步时nosetests --with-coverage,我得到了我宁愿忽略的各种模块的细节.但是我不能使用该
--cover-package=PACKAGE选项,因为foo.py&bar.py不在包中.(请参阅http://lists.idyll.org/pipermail/testing-in-python/2008-November/001091.html之后的帖子,
以了解我没有将它们放入包中的原因.)
我可以将coverage输出限制为foo.py和bar.py吗?
更新 - 假设没有比下面的Nadia更好的答案,我问了一个跟进问题:"如何编写一些(bash)shell脚本将目录中所有匹配的文件名转换为命令行选项? "
我一直在看到和阅读很多人使用鼻子来运行他们的Django测试.我无法弄清楚使用Nose运行我的Django测试的额外好处.如果有人可以填写我的鼻子是什么以及它如何为Django项目增加更多,那将会有所帮助.
我无法找到一份概述这些要点的好文件/文章.
谢谢
我正在使用nosetests,在两个单独的文件中,我有两个测试.单独运行时两者都运行正常,但是当一起运行时,第一次测试的模拟会在第二次测试中混淆结果.如何确保在测试功能完成后重置所有模拟/补丁,以便每次运行都能得到干净的测试?
如果可能的话,通过我的测试解释将特别感激.我的第一个测试看起来像:
def test_list_all_channel(self):
from notification.models import Channel, list_all_channel_names
channel1 = Mock();
channel2 = Mock();
channel3 = Mock();
channel1.name = "ch1"
channel2.name = "ch2"
channel3.name = "ch3"
channel_list = [channel1, channel2, channel3]
Channel.all = MagicMock()
Channel.all.return_value = channel_list
print Channel
channel_name_list = list_all_channel_names()
self.assertEqual("ch1", channel_name_list[0])
self.assertEqual("ch2", channel_name_list[1])
self.assertEqual("ch3", channel_name_list[2])
Run Code Online (Sandbox Code Playgroud)
我的第二个测试是:
def test_can_list_all_channels(self):
add_channel_with_name("channel1")
namelist = list_all_channel_names()
self.assertEqual("channel1", namelist[0])
Run Code Online (Sandbox Code Playgroud)
但Channel.all()的返回值仍然设置为第一个函数的列表,所以我得到"ch1"不等于"channel1".有什么建议?非常感谢!
nose ×10
python ×10
unit-testing ×5
mocking ×2
testing ×2
centos ×1
coverage.py ×1
django ×1
generator ×1
nosetests ×1
pycharm ×1
sqlalchemy ×1