From 5a5f374d00f9e7b9e319cc27e36eadf319d922f7 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sun, 2 Apr 2017 15:24:43 +0000 Subject: [PATCH] Test systems using tox --- .coveragerc | 28 +++++++++++++++++++ .gitignore | 1 + docs/tests.rst | 7 ++--- requirements/base.txt | 3 +- requirements/testing.txt | 2 ++ scent.py | 5 +--- .../apps/machines/tests/test_models.py | 5 ++-- .../apps/machines/tests/test_serializers.py | 20 +++++++++---- stagestatus/settings/base.py | 5 ++++ tox.ini | 12 ++++++++ 10 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 .coveragerc create mode 100644 tox.ini diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..82c46ee --- /dev/null +++ b/.coveragerc @@ -0,0 +1,28 @@ +# .coveragerc to control coverage.py +[run] +branch = True +include=stagestatus/*.* +omit = ../*migrations* + +[report] +# Regexes for lines to exclude from consideration +exclude_lines = + # Have to re-enable the standard pragma + pragma: no cover + + # Don't complain about missing debug-only code: + def __repr__ + if self\.debug + + # Don't complain if tests don't hit defensive assertion code: + raise AssertionError + raise NotImplementedError + + # Don't complain if non-runnable code isn't run: + if 0: + if __name__ == .__main__.: + +ignore_errors = True + +[html] +directory = cover diff --git a/.gitignore b/.gitignore index 31fe989..3788856 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ pip-log.txt # Unit test / coverage reports .coverage +cover .tox nosetests.xml diff --git a/docs/tests.rst b/docs/tests.rst index 9bab073..d7a1b57 100644 --- a/docs/tests.rst +++ b/docs/tests.rst @@ -13,10 +13,7 @@ This will install the fake model factory generator, and test runners. Running ------- -To run tests, simply use ``python manage.py test`` this will invoke Nose and pickup all the tests. - -To start the auto testing, just run ``sniffer`` from the root project directory, this will watch all files in ``stagestatus/`` for changes, and if any ``.py`` files are changed it will re-run the tests. - -You can pass ``-x appname`` to ``sniffer`` and it will run the appropriate tests for that app, but as of this writing, there is only one app. +To run tests, simply use ``tox`` this will instal all deps, and run the ``manage.py test`` command, invoke Nose and pickup all the tests. +To start the auto testing, just run ``sniffer`` from the root project directory, this will watch all files in ``stagestatus/`` for changes, and if any ``.py`` files are changed it will re-run the tests with ``tox``. diff --git a/requirements/base.txt b/requirements/base.txt index 190f56f..99475dd 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -6,7 +6,8 @@ Django django-model-utils -mysql-python +mysql-python; python_version < '2' +mysqlclient; python_version > '3' djangorestframework markdown # Markdown support for the browsable API. diff --git a/requirements/testing.txt b/requirements/testing.txt index 5e37033..94854ec 100644 --- a/requirements/testing.txt +++ b/requirements/testing.txt @@ -5,3 +5,5 @@ model-mommy django-nose pyinotify sniffer +coverage +tox diff --git a/scent.py b/scent.py index b3f85a5..b5f57b9 100644 --- a/scent.py +++ b/scent.py @@ -22,7 +22,4 @@ def py_files(filename): @runnable def execute_nose(*args): from subprocess import call - if len(args) > 1: - return call('python manage.py test %s' % args[1], shell=True) == 0 - else: - return call('python manage.py test', shell=True) == 0 + return call('tox', shell=True) == 0 diff --git a/stagestatus/apps/machines/tests/test_models.py b/stagestatus/apps/machines/tests/test_models.py index 0a04a79..312ebae 100644 --- a/stagestatus/apps/machines/tests/test_models.py +++ b/stagestatus/apps/machines/tests/test_models.py @@ -1,6 +1,6 @@ from __future__ import print_function -from django.test import TestCase +from django.test import TransactionTestCase from model_mommy import mommy @@ -9,7 +9,8 @@ from django.contrib.auth.models import User from machines.models import Machine, MachineStatus, MachineReservation -class ModelsTest(TestCase): +class ModelsTest(TransactionTestCase): + cleans_up_after_itself = True def setUp(self): self.user = mommy.make(User) diff --git a/stagestatus/apps/machines/tests/test_serializers.py b/stagestatus/apps/machines/tests/test_serializers.py index 8c72237..64298a5 100644 --- a/stagestatus/apps/machines/tests/test_serializers.py +++ b/stagestatus/apps/machines/tests/test_serializers.py @@ -1,6 +1,7 @@ from __future__ import print_function +import json -from django.test import TestCase +from django.test import TransactionTestCase from model_mommy import mommy from rest_framework.renderers import JSONRenderer @@ -13,7 +14,8 @@ from machines.models import Machine, MachineStatus, MachineReservation from machines.api.serializers import MachineSerializer -class SerializerTest(TestCase): +class SerializerTest(TransactionTestCase): + cleans_up_after_itself = True def setUp(self): self.user = mommy.make(User) @@ -28,8 +30,14 @@ class SerializerTest(TestCase): def test_serializer(self): """ - Test to make sure the serializer returns data in proper format + Test to make sure the api returns data in proper format, + not just the hostname and db_server """ - ms = MachineSerializer(self.machine) - ms_json = JSONRenderer().render(ms) - self.assertEqual("", ms_json) + machine = MachineSerializer(self.machine) + machine_json = JSONRenderer().render(machine.data) + machine_dict = json.loads(machine_json) + + keys = set(machine_dict.keys()) + assumed_keys = set((u'db_server', u'hostname', u'is_reserved', + u'reservation', u'status')) + self.assertEqual(assumed_keys, keys) diff --git a/stagestatus/settings/base.py b/stagestatus/settings/base.py index 8a9a90b..3308667 100644 --- a/stagestatus/settings/base.py +++ b/stagestatus/settings/base.py @@ -121,6 +121,11 @@ AUTH_PASSWORD_VALIDATORS = [ ] TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' +NOSE_ARGS = [ + '--with-coverage', + '--cover-html', + '--cover-erase', +] try: from .local import * # noqa diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..4cfd2e2 --- /dev/null +++ b/tox.ini @@ -0,0 +1,12 @@ +[tox] +;don't require setup.py +skipsdist=true +envlist=py27 + +[testenv:py27] +commands= + python manage.py test +setenv= + DJANGO_SETTINGS_MODULE=stagestatus.settings +deps= + -rrequirements/testing.txt