Monthly Archives: October 2014

This blog is a follow up on my previous post here. As I mentioned my testing strategy at KuKy World, we use Lettuce for testing, and for each installed app, we will test for both back-end behavior and web browser behavior. As we have already addressed the issue of how to switch to test database for back-end test, I will discuss how to switch to test database for browser behavior testing.

Since we use SQLAlchemy, the session is created at the global level. However, the session will be imported in each different view file, and if webdriver opens up a URL, it will use the session which has been created at the global level, which is usually using the development database.

The way we are dealing this issue is not too hard, the idea is to make the session use test database at the global level when python mange.py harvest gets called. The following is the piece of code in our local settings file that does the job.

DB_ENVIRONMENT = 'mysql://root:@localhost/development'

# This is for browser behavior testing on local
if "harvest" in sys.argv:
    DB_ENVIRONMENT = 'mysql://root:@localhost/test'

engine = sqlalchemy.create_engine(DB_ENVIRONMENT)
Session = sqlalchemy.orm.sessionmaker(bind=engine)
session = Session()
Base = declarative_base()

Now, when you run python manage.py harvest, it will start using the test database, and python manage.py runserver will keep using the development database.

Thanks for reading.

Advertisements

I haven’t started doing any automation testing, and I know it is important; therefore, I start thinking about a good way to do tests, being more specific, at this stage, I want to focus on browser behavior testing. I quickly found out something called Watir WebDriver, which is in Ruby. Since everything here is written in Python, although personally I want to learn some Ruby, it will become very hard if I will have any testing related to database. Therefore, I kept looking until I found Lettuce. Lettuce has built-in integration with Django, however for some of the features to work, it assumes that you are using the default Django ORM, which is not the case here at KuKy World because we use SQLAlchemy.

Now, I am going to talk what we do to switch between our test and development database. First of all, my testing strategy is the following: I have a features folder under each of my registered app, and in each features folder, I have two sub folders: backend, which is for testing on the back end, and web, which is testing on the browser. The file structure will look this:

Screen Shot 2014-10-05 at 10.55.58 AM

In order to make the testing use test database, I will have to define the test database environment in terrain.py file, the following is what I did:

from lettuce import world
import sqlalchemy, sqlalchemy.orm
from sqlalchemy.ext.declarative import declarative_base

def recrete_test_db():
    testing_db = 'mysql://root:@localhost/test'
    engine = sqlalchemy.create_engine(testing_db)
    Session = sqlalchemy.orm.sessionmaker(bind=engine)
    world.session = Session()
    Base = declarative_base()
    # import all the models
    from webapp.models import *
    # Re create the tables in test database
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)

world.recrete_test_db = recrete_test_db

and then in my signup.py file, you can recreate your test database anywhere based on your need, here is an example:

from lettuce import *

@before.all
def say_hello():
    print 'Hello there!'
    print 'Lettuce will recreate the test database and run tests...'
    world.recrete_test_db()

Note: when you communicate with your test database in tests, you will be using world.session rather than other sessions you might have at your global level.

Thanks for reading.