有助于为django编写自动化测试的代码段。
django-smarttest的Python项目详细描述
目标
提供有助于在django中运行测试的代码段。
安装
安装要求:
pip install -r requirements.txt
安装SmartTest:
pip install django-smarttest
或当前开发版本:
pip install hg+https:://bitbucket.org/kidosoft/django-smarttest
配置
INSTALLED_APPS=[...'smarttest',...]
用法
防止测试接触数据库
importunittestfromsmarttest.decoratorsimportno_db_testcase@no_db_testcaseclassSomeTestCase(unittest.TestCase):deftest_some_test(self):...
如果你不小心写了一些代码,试图在数据库上运行一些查询 你会有例外的。
仅运行选定的测试类型
importunittestfromsmarttest.decoratorsimporttest_type@test_type('acceptance')classSomeAcceptanceTestCase(unittest.TestCase):deftest_some_acceptance_test(self):...@test_type('unit')classSomeUnitTestCase(unittest.TestCase):deftest_some_unit_test(self):...classUnspecifiedTypeTestCase(unittest.TestCase):deftest_some_test(self):...
$python-munittestscript...----------------------------------------------------------------------Ran3testsin0.000sOK$IGNORE_TESTS=unitpython-munittestscript.s.----------------------------------------------------------------------Ran3testsin0.000sOK(skipped=1)$IGNORE_TESTS=acceptancepython-munittestscripts..----------------------------------------------------------------------Ran3testsin0.000sOK(skipped=1)$IGNORE_TESTS=acceptance,unitpython-munittestscriptss.----------------------------------------------------------------------Ran3testsin0.000sOK(skipped=2)
测试类型可以是任何选定的单词。它不一定是“单位”或“接受”。如果需要,可以使用不同的测试类型在不同的环境中运行。
与可切换的碎片驱动器一起使用的测试用例
允许在不同驱动程序之间简单切换的简单测试用例 在测试中。
在settings.py中:
TEST_DRIVER='firefox'# or django or any other
测试中:
fromsmarttest.testcasesimportSplinterTestCaseclassSomeTestCase(SplinterTestCase):deftest_some_test(self):self.browser.visit(self.get_host()+'/')
如果您连续运行测试(例如,执行测试驱动开发) test_driver='django'(默认设置)是最快的驱动程序 中断您的工作流程。但是,当您运行验收测试时,您可能需要 检查它在真实浏览器中的行为。只需设置test_driver='firefox' 或其他任何由Splinter支持的真正浏览器。
支持的django版本
测试时间:
- Python2.7上的django 1.2.7
- Python2.7上的django 1.3.7
- Python2.7上的django 1.4.16
- Django 1.5.11Python2.7、Python3.3、Python3.4
- Django 1.6.8Python2.7、Python3.3、Python3.4
- Django 1.7.1Python2.7、Python3.3、Python3.4