How to Write Tests - adaptive-learning/flocs GitHub Wiki
Frontend Tests
- We use Jasmine framework to write tests. To learn Jasmine syntax go here.
- Name your tests with the .spec.js postfix, e.g.
maze.drv.js
->maze.drv.spec.js
- Place your tests in the SAME directory, as the tested file
- To learn how to test Controllers, Filters and Directives, go here .
- To learn mocking the objects go here .
- Divide tests to more describe blocks
- use
make test-frontend
to run tests
Testing filter
describe('Testing the specified filter', function() {
// load the angular module, which contains the filter
beforeEach( module('moduleName'));
it('Bief description of test',
// IMPORTANT: append 'Filter' to the name of the filter
inject(function(nameOfFilterFilter) {
// prepare data
var variable1 = 4;
var variable2 = 5;
expect(nameOfFilterFilter(variable1, variable2)).toBe(variable1 * variable2);
}));
});
Backend tests
- For backend unit testing, we use Python standard library module
unittest
and Django moduledjango.test
(which provides some subclasses of the standardunittest.TestCase
, e.g. to allow for interaction with DB in isolation). - Name your tests with the
test_{module}.py
, where{module}
is name of the module you are unit testing, e.g.test_task_model.py
. (This is important for tests to be automatically discovered.) - Place the test file in the same directory as the tested file.
- Read introduction to writing and running tests in Django.
- For motivation for automated testing and examples of some tests, see Django tutorial 05.
- For testing of views, testing with live server, fixture loading, skipping tests and more see Django testing tools.
- Use
make test-backend
to run all backend tests. (If you want to run just some test, you will need./manage.py test
command.)
Testing models
Example from documentation:
from django.test import TestCase
from myapp.models import Animal
class AnimalTestCase(TestCase):
def setUp(self):
Animal.objects.create(name="lion", sound="roar")
Animal.objects.create(name="cat", sound="meow")
def test_animals_can_speak(self):
"""Animals that can speak are correctly identified"""
lion = Animal.objects.get(name="lion")
cat = Animal.objects.get(name="cat")
self.assertEqual(lion.speak(), 'The lion says "roar"')
self.assertEqual(cat.speak(), 'The cat says "meow"')
You can load data for testing from fixtures (but make sure these are special fixtures for testing only, so they are not changing over time).
from django.test import TestCase
from myapp.models import Animal
class AnimalTestCase(TestCase):
fixtures = ['test_mammals', 'test_birds']
def setUp(self):
# Test definitions as before.
call_setup_methods()
def testFluffyAnimals(self):
# A test that uses the fixtures.
call_some_test_code()
Testing services
You can use the same approach as for testing models. Strictly speaking, it becomes an integration test (because it tests a model as well), but it's easier than mocking model managers and discovers errors which a unit test would not. For really complex business behavior (beahavior in the domain model), I recommend to move it to functions/classes which don't depend on model managers, so they can be easily tested separately.
Testing views
See an example in Django tutorial 05. More deatils in testing tools.