应付Django 1.3.1 Unit test的一个Bug
资源版本信息:
Python:2.7
Django:1.3.1
DB:Postgresql
pycopg2:2.4.4
OK,如果你是使用和我一样的配置,恭喜你,你会得到和我一样的单元测试问题
作者:Wally Yu
CSDN:http://blog.csdn.net/quicktest
微博:http://www.weibo.com/quicktest
问题描述:
运行单元测试:
python manage.py test
得到报错信息:
psycopg2.programmingerror autocommit cannot be used inside a transaction
网上查了一下,是因为Django1.3.1对pscopg2的2.4.2及以上版本不兼容
解决办法(本人在windowsXP和Ubuntu11上均实践通过):
方法一:
升级Django至最新版本 1.4
方法二:
降级psycopg2到2.4.1版本:
- Windows:下载对应的安装包,地址:http://www.stickpeople.com/projects/python/win-psycopg/index.2.4.1.html
- Linux:运行如下命令:
sudo apt-get update
sudo apt-get install python-setuptools python-pip build-essential python-dev libpq-dev
sudo pip install psycopg2==2.4.1
至此,再次运行
python manage.py test
可以得到可爱的unit test 结果:
Windows:
D:\Projects\CloudTesting>python manage.py test
Creating test database for alias 'default'...
................................................................................
................................................................................
................................................................................
................................................................................
......
----------------------------------------------------------------------
Ran 326 tests in 17.266s
OK
Destroying test database for alias 'default'...
Linux:
root@SHA-Essqa-Linux:~/project/CloudTesting# python manage.py test
Creating test database for alias 'default'...
......................................................................................................................................................................................................................................................................................................................................
----------------------------------------------------------------------
Ran 326 tests in 20.123s
OK
Destroying test database for alias 'default'...
大功告成!!!