假设我有这个python代码:
def answer():
ans = raw_input('enter yes or no')
if ans == 'yes':
print 'you entered yes'
if ans == 'no':
print 'you entered no'
Run Code Online (Sandbox Code Playgroud)
我该如何为此编写单元测试?我知道我必须使用'模拟',但我不明白如何.有谁可以做一些简单的例子?
我尝试为python安装couchbase,但是我收到以下错误:
building 'couchbase._libcouchbase' extension
creating build/temp.linux-i686-2.7
creating build/temp.linux-i686-2.7/src
creating build/temp.linux-i686-2.7/src/viewrow
creating build/temp.linux-i686-2.7/src/contrib
creating build/temp.linux-i686-2.7/src/contrib/jsonsl
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes - fPIC -I/usr/include/python2.7 -c src/exceptions.c -o build/temp.linux-i686-2.7/src/exceptions.o
In file included from src/exceptions.c:17:0:
src/pycbc.h:25:36: fatal error: libcouchbase/couchbase.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
我安装了couchbase服务器和c库,我已经有了:
sudo apt-get install build-essential # for a C compiler
sudo apt-get install python-dev
Run Code Online (Sandbox Code Playgroud)
那有什么不对?
我有这个观点:
def send_results(request):
print request
if request.is_ajax():
address = request.POST.get('url')
process_data(address)
context = get_all_from_database()
return HttpResponse(json.dumps(context), content_type='application/json')
Run Code Online (Sandbox Code Playgroud)
我需要测试一下:
def test_send_results(self):
factory = RequestFactory()
request = factory.get('/views/send_results')
response = send_results(request)
self.assertEqual(response.status_code, 200)
Run Code Online (Sandbox Code Playgroud)
但它总是在错误中停止,在我的视图中,"地址"值在重新排列之前被重新引用.如何传递一些价值?
我有一个python函数,必须从文件中读取数据并将其拆分为两个键和值,然后将其存储在字典中.示例:file:
http://google.com 2
http://python.org 3
# and so on a lot of data
Run Code Online (Sandbox Code Playgroud)
我使用split函数,但是当有很多数据时它会引发值错误
ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
这是失败的确切代码
with open(urls_file_path, "r") as f:
for line in f.readlines():
url, count = line.split()# fails here
url_dict[url] = int(count)
Run Code Online (Sandbox Code Playgroud)