小编use*_*971的帖子

python模拟unittests中的原始输入

假设我有这个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 unit-testing mocking

33
推荐指数
4
解决办法
3万
查看次数

尝试安装Couchbase,gcc命令失败,Python

我尝试为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)

那有什么不对?

python gcc couchbase

8
推荐指数
1
解决办法
5268
查看次数

Django用数据查看测试,RequestFactory

我有这个观点:

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 django unit-testing

4
推荐指数
1
解决办法
3085
查看次数

Python拆分功能.解压错误的值太多

我有一个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)

python string

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×4

unit-testing ×2

couchbase ×1

django ×1

gcc ×1

mocking ×1

string ×1