我正在尝试在已经存在的类中测试一个方法。在类的方法inputStreamThread
中调用该方法。Foo.crawler.crawlerapp.CrawlerApp
addUrl
inputStreamThread
从标准输入读取然后调用addUrl
addUrl
也在CrawlerApp
课堂上
我希望能够assert_called_with
在模拟上使用addUrl
来检查inputStreamThread
是否在做正确的事情并打电话addUrl
问题是我无法为 inside 的模拟获得正确的addUrl
语法CrawlerApp
我直接从模拟文档中使用了一个示例,但得到如下所示的错误
如您所见,我也在模拟标准输入以便能够在其上显示测试数据
我的问题是,我使用什么代码来执行这种测试而不显示错误?
import Foo.crawler.crawlerapp
from unittest import TestCase
from mock import patch, Mock
from mephistopheles.messageformat import EventDataFrame
from mephistopheles.messageformat.types import adservers as pbufs
import time
import sys
class testDeserial(TestCase):
def generate_dummy_auction_event(self,url):
adunitinfo = pbufs.AdUnitInfo(index_on_page=0, url=url)
geoloc = pbufs.GeoLocation(country="DE", region="low")
userinfo = pbufs.UserInfo(user_hash=1,
ip_octets=1,
geolocation=geoloc,
language="en")
auctioninfo = pbufs.AuctionInfo(timestamp=int(time.time()),
user=userinfo,
ad_unit=adunitinfo)
return auctioninfo
def setUp(self):
pass
@patch.object(Foo.crawler.crawlerapp.CrawlerApp,'addUrl')
def test_check_url(self, MaddUrl):
url_a = "http://audaxing.wordpress.com"
dummy_event = self.generate_dummy_auction_event(url_a)
with patch("sys.stdin") as mock_stdin:
mock_stdin.read.return_value = dummy_event
ca._running = True
input_thread = threading.Thread(target=self.inputStreamThread)
input_thread.start()
time.sleep(0.5)
ca._running = False
MaddUrl.assert_called_with(url_a)
测试运行输出....
$ bin/tests --tests-pattern=test_deserialize
Test-module import failures:
Module: Foo.crawler.tests.test_deserialize
Traceback (most recent call last):
File "/home/jamie/svn/Foo/crawler.buildout/trunk/src/Foo.crawler/Foo/crawler/tests/test_deserialize.py", line 11, in <module>
class testDeserial(TestCase):
File "/home/jamie/svn/Foo/crawler.buildout/trunk/src/Foo.crawler/Foo/crawler/tests/test_deserialize.py", line 28, in testDeserial
@patch.object(Foo.crawler.crawlerapp.CrawlerApp,'addUrl')
AttributeError: 'function' object has no attribute 'object'
Test-modules with import problems:
Foo.crawler.tests.test_deserialize
Total: 0 tests, 0 failures, 0 errors in 0.000 seconds.