レタスは、Django アプリの BDD テスト フレームワークとして非常に優れているようです。ただし、それを使用してモデルをテストする方法の例やドキュメントは見つかりませんでした。利用可能なものはありますか?
2 に答える
同じものを探していましたが、適切なドキュメントやチュートリアルが見つかりませんでした。モデルの検証をテストし、関係の健全性をチェックするために、シナリオから値を入れてフェッチし、それらを検証してみました。それは、私が推測するモデルから検証するために必要な検証の多くです。
この投稿は少し前に作成されたようですが、私にとっては結果のトップだったので、ここに私の発見があります.
Lettuce にはDjango 用のデコレータが@before.runserver
あり@after.runserver
、terrain.py ファイルにテスト データベースを実装するために使用できます。
この例では SQLite データベースを使用しており、South [:(] も使用しているため、SOUTH_TESTS_MIGRATE
が に設定されていることを確認する追加のテストFalse
があります。local_settings_test.py ファイルがあり、テストに固有の設定で上書きされます。次のように、収穫コマンドで大文字と小文字を区別して呼び出します。
python manage.py harvest --settings=local_settings_test
これが私のセットアップと破壊の呼び出しです。もちろん、たとえば、すべての機能、シナリオ、またはステップでデータベースをリセットしたい場合は、これらを他のデコレータで実装できます。利用可能なものの詳細については、 http://lettuce.it/reference/terrain.htmlを参照してください。
from lettuce import *
from django.conf import settings
from django.core.management.base import CommandError
from django.core.management import call_command
def assert_test_database():
"""
Raises a CommandError in the event that the database name does not contain
any reference to testing.
Also checks South settings to ensure migrations are not implemented.
"""
if not '-test' in settings.DATABASES['default']['NAME']:
raise CommandError('You must run harvest with a test database')
if getattr(settings, 'SOUTH_TESTS_MIGRATE', True):
raise CommandError('SOUTH_TESTS_MIGRATE should be set to False')
@before.runserver
def create_database(server):
"""
Asserts the database name is correct and creates initial structure, loading
in any test_data fixtures which may have been created.
"""
assert_test_database()
call_command('syncdb', interactive=False, verbosity=0)
call_command('loaddata', 'test_data', interactive=False, verbosity=0)
@after.runserver
def flush_database(server):
"""
Asserts the database name is correct and flushes the database.
"""
assert_test_database()
call_command('flush', interactive=False, verbosity=0)
これらのステップをテレイン ファイルに追加したら、単体テストの場合と同様に、ステップでモデルを呼び出すことができます。