Skip to content

ramonnst/cdl_tests

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CDL Free for all

Testing

Demo

TestCase

  • Packages: unittest, unittest2
  • Runner: nose
  • Command: nosetests test_user.py
class TestUser(unittest.TestCase):

    def test_user_attributes(self):
        pass

Run the test, see it passes, nosetests test_user.py

Model

  • Describe a User model:
class User(object):
    """Create a object with the expected attributes.
    We will transform it in a Django model later.
    """

    def __init__(self, firstname, lastname, password):
        self.firstname = firstname
        self.lastname = lastname
        self.password = password

Import the class in the console, create an object of type User.

Factory

  • Package: factory-boy
  • Describe a factory for the User model:
import factory


class UserFactory(factory.Factory):
    # Generate random strings for Users names, like: Name-1
    firstname = factory.Sequence(lambda n: "FirstName-%s" % str(n))
    lastname = factory.Sequence(lambda n: "LastName-%s" % str(n))

    # Create the same password for all users
    password = "Password"

Create Django project and app

  • Check if you have Django, otherwise install Django
  $ pip freeze | grep Django
  $ sudo pip install Django
  • Create Django project and app
$ django-admin.py startproject cdl_tests
$ cd cdl/
$ django-admin.py startapp cdl
  • Add User as a Django model:
    • $ cd cdl_tests/cdl/
    • open and edit models.py
from django.db import models


class User(models.Model):
    firstname = models.CharField(max_length=30)
    lastname = models.CharField(max_length=30)
    password = models.CharField(max_length=30)

As you can see we don't have a __init__ method, we just define the model attributes as class attributes.

  • Copy the factory class in a new factories file:
$ touch ~/cdl_tests/cdl/factories.py

and copy the class inside the file, example.

  • Create the application Database and sync the models
  $ cd cdl_tests
  $ vim cdl_tests/settings.py
  • Update the project database configuration and installed apps lists, example
  • Sync the database: $ python manage.py syncdb

Copy and run the tests

  • Copy the test case inside the project in the file ~/cdl_tests/cdl/tests.py, example

  • Run all the project tests, with the command: python manage.py test

The runner is build in Django, no need to use nosetests anymore.

If you have any questions open an issue.

Thanks! :)

About

Presentation & Demo for CDL Test course

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published