Lettuce具有若干Django內(nèi)置步驟,簡化定制器的創(chuàng)建夺艰。
創(chuàng)建定制器數(shù)據(jù)
Lettuce可以自動(dòng)對你可用的Django模型創(chuàng)建定制器數(shù)據(jù)紧索,例如
Background:
Given I have options in the database:
| name | value |
| Lettuce | Rocks |
能發(fā)現(xiàn)一個(gè)名字叫options的模型咖气。它將使用表中指定的參數(shù)創(chuàng)建該模型的對象(name=Lettuce, value=Rocks)。
還可以這樣指定關(guān)系信息汗洒。假設(shè)具有外鍵user和字段avatar的模型Profile:
Background:
Given user with username "harvey" has profile in the database:
| avatar |
| cat.jpg |
要?jiǎng)?chuàng)建多對多關(guān)系议纯,假設(shè)User屬于多個(gè)Group對象:
Background:
Given user with username "harvey" is linked to groups in the database:
| name |
| Cats |
要?jiǎng)?chuàng)建多對多關(guān)系,這兩個(gè)模型必須在鏈接之前存在溢谤。
大部分常見的數(shù)據(jù)可以被解析瞻凤,即真/假、數(shù)字世杀、字符串和日期的形式2013-10-30阀参。
注冊自己的模型構(gòu)造器
對于更復(fù)雜的模型,處理和分析數(shù)據(jù)瞻坝,你可以使用creates_models裝飾自己寫的創(chuàng)建步驟蛛壳。
from lettuce.django.steps.models import (creates_models,
reset_sequence,
hashes_data)
@creates_models(Note)
def create_note(step):
data = hashes_data(step)
for row in data:
# convert the author into a user object
row['author'] = get_user(row['author'])
Note.objects.create(**row)
reset_sequence(Note)
測試模型
兩個(gè)步驟來測試模型。
Then features should be present in the database:
| name | value |
| Lettuce | Rocks |
And there should be 1 feature in the database
你也可以通過前綴@的屬性名稱測試非數(shù)據(jù)庫模型屬性所刀。在從數(shù)據(jù)庫中選擇記錄之后測試非數(shù)據(jù)庫屬性衙荐。
Then features should be present in the database:
| name | value | @language |
| Lettuce | Rocks | Python |
注冊自己的模型測試
對于更復(fù)雜的測試,處理和分析數(shù)據(jù)浮创,你可以使用checks_existence裝飾你自己的步驟忧吟。
測試電子郵件
有6個(gè)步驟,允許你對發(fā)送電子郵件進(jìn)行比較全面的測試斩披,僅需要你使用Django默認(rèn)django.core.mail功能溜族。
檢查發(fā)送的郵件數(shù)量:
Then I have sent 1 email
還有一個(gè)更易讀的步驟,用于檢查沒有發(fā)送郵件:
Then I have not sent any emails
檢查電子郵件正文中是否包含以下多行字符串:
Then I have sent an email with the following in the body:
"""
Lettuce is a BDD tool for python, 100% inspired on cucumber.
"""
檢查電子郵件的一部分(主體垦沉、主體煌抒、from_email,乡话,BCC摧玫,CC)是否包含給定文本:
Then I have sent an email with "Lettuce" in the body
你應(yīng)該一直測試你的功能的失敗案例耳奕。因此绑青,有步驟確保發(fā)送電子郵件按預(yù)期失敗诬像。這將導(dǎo)致smtpexception總是被提出:
Given sending email does not work
有些測試點(diǎn),你可能會(huì)想清除你以前所有的有變化的發(fā)件箱闸婴。清除你的電子郵件坏挠,并重置之前sending email does not work步驟造成的任何破壞,可以使用:
Given I clear my email outbox
希望在每次測試之后邪乍,運(yùn)行清理降狠。要做到這一點(diǎn),只要在你terrain.py添加:
::
from lettuce import after, before from lettuce.django.steps.mail import mail_clear
@before.each_background def reset_email(lettuce_object):
mail_clear(lettuce_object)
settings.py變量
LETTUCE_APPS–測試默認(rèn)應(yīng)用程序
LETTUCE_USE_TEST_DATABASE–使用測試數(shù)據(jù)庫代替實(shí)時(shí)數(shù)據(jù)庫庇楞。等值t標(biāo)志榜配。
其他參考變量
SOUTH_TESTS_MIGRATE – 使用south遷移應(yīng)用到測試數(shù)據(jù)庫
上一篇:Lettuce: Language Support
下一篇:Lettuce: Use nose for pretty assertions