我有兴趣学习如何以更敏捷/ BDD的方式进行Doctests和Unit测试.我发现了一些看似合理的教程,但它们只是缩略图.我真正希望看到的是一些开发了BDD风格的Django项目的源代码.
我不清楚的事情是你如何处理请求对象等我有一种情况,我已经部署了我的应用程序,我在生产中得到了完全不同的行为,我在开发中甚至从生产中的Python shell服务器.我希望一些Doctests可以帮助我诊断这个问题,并为更加敏捷的编写测试过程打开大门.
具体来说,这是我试图测试的代码:
def match_pictures_with_products( queryset, number_of_images = 3):
products = []
i = 0
for product in queryset:
if i < ( number_of_images ):
image = product.imagemain_set.all()[:1]
product.photo_url = image[0].photo.url
products.append(product)
i += 1
return products
def index(request):
"""returns the top 10 most clicked products"""
products = Product.objects.all()[:10]
products = match_pictures_with_products( products, 10) .
return render_to_response('products/product_list.html', {'products': products})
Run Code Online (Sandbox Code Playgroud)
如何创建一个Doctest来确保索引返回10个对象?
产品查询似乎可以从生产服务器上的shell中正常工作.实际的服务器根本没有返回任何产品.
我正在尝试测试是否在新用户在django_authopenid中注册时创建了UserProfile模型.我不明白如何将Openid会话数据添加到POST.
class UserTestCAse(TestCase):
def test_register_should_create_UserProfile(self):
from django.test.client import Client
c = Client()
response = c.post('/account/register/', {u'username': [u'john'], u'email': [u'john@beatles.com'], u'bnewaccount': [u'Signup']},)
self.assertEqual(response.status_code, 302)
user = User.objects.get( username ='john')
self.assertTrue(user.get_profile())
Run Code Online (Sandbox Code Playgroud) 如何针对生产数据库而不是测试数据库运行单元测试?
我有一个错误,似乎发生在我的生产服务器上,但不在我的开发计算机上.
我不在乎数据库是否被破坏.
我喜欢在提交前运行测试。我是Selenium的新手,我不了解如何运行测试并且不更改数据库。
我的本地数据库有许多相同的问题。
有什么办法可以让我运行这些测试,而又没有将数据库还原到tearDown上的原始状态?
from selenium import webdriver
from django.utils import unittest
from selenium.webdriver.support.ui import WebDriverWait
class TestAuthentication(unittest.TestCase):
scheme = 'http'
host = 'localhost'
port = '4444'
def setUp(self):
self._driver = webdriver.Firefox()
self._driver.implicitly_wait(5)
def login_as_Bryan(self):
self._driver.get('http://localhost:8000/account/signin/')
user = self._driver.find_element_by_id('id_username')
user.send_keys("Bryan")
password = self._driver.find_element_by_id('id_password')
password.send_keys('***************')
submit = self._driver.find_element_by_id('blogin')
submit.click()
def test_user_should_be_able_to_login_manually(self):
self.login_as_Bryan(self)
message = self._driver.find_element_by_class_name('darkred')
self.assertEqual("Welcome back Bryan, you are now logged in", message.text)
def test_Bryan_can_post_question(self):
self.login_as_Bryan()
self._driver.find_element_by_link_text("ask a question").click()
self._driver.find_element_by_id('id_title').send_keys("Question should succeed")
self._driver.find_element_by_id('editor').send_keys("This is the body text.")
self._driver.find_element_by_id('id_tags').send_keys("test")
self._driver.find_element_by_class_name("submit").click()
self.assertTrue(self._driver.find_element_by_link_text("Question should …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置Eclipse以运行PyDev为GAE开发,但我不知道该链接到解释器。
我正在链接到包含/ lib的Google App引擎目录google-cloud-sdk /文件夹,但出现错误:
Invalid Google App Engine directory. Did not find: appcfg.py in /Users/bryanwheelock/Downloads/google-cloud-sdk
Run Code Online (Sandbox Code Playgroud)
我在计算机上运行“查找”命令,但找不到任何appcfg.py实例
被遗忘了吗?
我正在尝试自定义Django Admin.
models.py
=============
class Question(models.Model):
poll = models.ForeignKey(Poll)
name = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
admin.py
===========
class QuestionAdmin(admin.ModelAdmin):
list_display = ('name', 'poll'. 'pub_date')
inlines = [ChoiceInline]
admin.site.register(Question)
Run Code Online (Sandbox Code Playgroud)
这似乎是自定义QuestionIndex的正确设置.
我希望这个显示:
你的问题是什么?introPoll 2009年7月31日
但是,唯一的默认unicode显示在Question索引上.
我错过了一步吗?
可能是某些原因导致附加数据未显示在索引上?
出于某种原因,每次我尝试使用easy_install安装新模块时,我都会收到错误:
AttributeError: 'module' object has no attribute '__getstate__'
Run Code Online (Sandbox Code Playgroud)
我正在使用setuptools-0.6c11-py2.6
我正在尝试删除96k记录.
此查询返回91k记录:
SELECT *
FROM xoops_bb_posts_text t
WHERE not exists (
select post_id
from xoops_bb_posts p
WHERE p.post_id = t.post_id
);
Run Code Online (Sandbox Code Playgroud)
当我试图删除那些记录时,我得到了语法错误,但我没有看到它.
DELETE FROM xoops_bb_posts_text t
WHERE not exists (
select post_id
from xoops_bb_posts p
WHERE p.post_id = t.post_id
);
Run Code Online (Sandbox Code Playgroud)
错误在哪里?
错误
SQL查询:文档
DELETE FROM xoops_bb_posts_text t
WHERE NOT EXISTS (
SELECT post_id
FROM xoops_bb_posts p
WHERE p.post_id = t.post_id
)
Run Code Online (Sandbox Code Playgroud)
MySQL说:文档
#1064 - 您的SQL语法有错误; 检查与您的MySQL服务器版本对应的手册,以便在'WHERE not exists'附近使用正确的语法(从第2行的xoops_bb_posts p WHERE p.post_id = t.post_中选择post_id)
我正在 Selenium Webdriver 中为我的 Django 应用程序创建单元测试。
我有一个从数据库中删除主题的 AJAX 方法。我想弄清楚如何验证已删除的主题是否不再出现在页面上。
我试图捕捉当 Webdriver 找不到元素时应该生成的异常:selenium.common.exceptions.NoSuchAttributeException
相反,我看到了一个错误:
*** URLError: <urlopen error timed out>
Run Code Online (Sandbox Code Playgroud)
这是我设置测试的方式:
from selenium import webdriver
from django.utils import unittest
class TestAuthentication(unittest.TestCase):
scheme = 'http'
host = 'localhost'
port = '4444'
def setUp(self):
self._driver = webdriver.Firefox()
self._driver.implicitly_wait(10)
def login_as_Kallie(self):
# Kallie has manual login
self._driver.get('http://localhost:8000/account/signin/')
user = self._driver.find_element_by_id('id_username')
user.send_keys("Kallie")
password = self._driver.find_element_by_id('id_password')
password.send_keys('*********')
submit = self._driver.find_element_by_id('blogin')
submit.click()
def test_Kallie_can_delete_topic(self):
self.login_as_Kallie()
self._driver.find_element_by_link_text("Topic to delete").click()
self._driver.find_element_by_xpath("/html/body/div/div[4]/div/div/div[2]/div/table/tbody/tr/td[2]/div/div[3]/span[5]/a").click()
self._driver.find_element_by_class_name('dialog-yes').click()
self._driver.get("http://localhost:8000/questions/")
# this is the Python exception …Run Code Online (Sandbox Code Playgroud) 尝试在osX Maverics 10.9.5上使用Python2.7.8从GAE SDK 1.95导入测试平台,我收到InvalidCertificateException错误.
from google.appengine.ext import testbed
File "/usr/local/google_appengine/google/appengine/ext/testbed/__init__.py", line 120, in <module>
from google.appengine.api import urlfetch_stub
File "/usr/local/google_appengine/google/appengine/api/urlfetch_stub.py", line 34, in <module>
_fancy_urllib_InvalidCertException = fancy_urllib.InvalidCertificateException
AttributeError: 'module' object has no attribute 'InvalidCertificateException'
Run Code Online (Sandbox Code Playgroud)
我查看了fancy_url模块,InvalidCertificateException类就在那里,所以我不明白它为什么不导入.
显然其他人也有同样的错误,所以我试图通过删除:urlfetch_cacerts.txt AND cacerts.txt来纠正它:
GoogleAppEngineLauncher/Contents/Resources/GoogleAppEngineDefault.bundle/Content??s/Resources/google_appengine/lib/cacerts/
Run Code Online (Sandbox Code Playgroud) django ×7
python ×3
unit-testing ×3
python-2.7 ×2
selenium ×2
agile ×1
django-admin ×1
doctest ×1
easy-install ×1
eclipse ×1
mysql ×1
pydev ×1
sql ×1
webdriver ×1