django
で
django-nose
を
テスト
個人的に
前提
django
をdjango-nose
を
Tips
1. Testクラスは どこに 置けば 良いか?
{Django_Project_Home}/{Target_App}/tests
配下に、
__init__.py
と
__init__.py
には
def setup_package(): print('') print(__name__, '__init__.py : setup_package() ========================================') def teardown_package(): print(__name__, '__init__.py : teardown_package() =====================================')
2. 標準出力にTest
の ログを 出力する。
test
コマンドでtest
にprint
の
python manage.py test
-s
オプションをprint
の
python manage.py test -s
settings.py
のNOSE_ARGS
に'--nocapture''--nologcapture',
の
NOSE_ARGS = [ ....... '--nocapture', '--nologcapture', ]
参考サイトは、NOSE_ARGS
にlogger
使うかと
-s
オプションに
3. テストデータを 投入したい。
sqlite
を
開発環境とは
データが
以下のように
import unittest from django.core import management from analyze_metrics_view.forms import get_category_choicese_relations, get_veiw_choices_relations class TestSelectViewForm(unittest.TestCase): @classmethod def setup_class(clazz): print(__name__, ": setup_class") management.call_command('loaddata', 'fixture/initial_data.json', verbosity=0) pass # このクラスのテストケースをすべて実行した後に1度だけ実行する @classmethod def teardown_class(clazz): print(__name__, ": teardown_class") management.call_command('flush', verbosity=0, interactive=False) pass
django-nose
のdjango
の
テストデータを
initial_data
は
参考サイト(
initial_data
の作成)
Providing initial data for models | Django documentation | Django参考サイト(テスト中の
データ投入)
How to load fixtures only once in django unit tests ? - Stack Overflow
4. 辞書を Assert
したい。
参考サイトのみ
副作用なかったのでassert all
の
- 参考サイト
Pythonでdictを 比較して assertしたい - Life is Really Short, Have Your Life!! python - Assert that two dictionaries are almost equal - Stack Overflow
5. Tupleを Assert
したい。
nose.tools
のassert_equals
でassert
できました。
2次元Tuple
だったのですが、
nose.tools.assert_equals(expect, result)
6. Testcase
が unittest.TestCase
を 継承するかしないかで、 setupメソッドの 挙動が 変わる。
辞書のassert
のunittest.TestCase
を
setup
メソッドが
import unittest class TestSelectViewForm(unittest.TestCase): # このクラスの各テストケースを実行する前に実行する def setup(self): print(__name__, ": setup") pass # このクラスの各テストケースを実行した後に実行する def teardown(self): print(__name__, ":teardown") pass
setup
をsetUp
、teardown
をtearDown
にcall
されるようになりました。
継承すると
しないとどちらのsetup
、
なるべく
※まあ、、
以上です。
コメント