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.