noseturbogears2

Disabling tgscheduler while running nosetests


I have a TurboGears 2.3.6 project that I've been working on for quite some time. In my project I'm using tgscheduler.

I want to learn how to use tests, and I have some difficulties starting. When I run

nosetests –v

I get this error for all of the default tests that comes with Turbogears:

ValueError: A task with the name NameOfMyTask already exists

And the test fails.

Can I tell nose to ignore tgscheduler somehow?

Thanks


Solution

  • The test suite in TurboGears creates a new application instance for each test, so that tests are run in a separated and isolated environment. For this reason AppGlobals are created multiple times (one for each application).

    While the documentation states to start the scheduler in AppGlobals.__init__, which works in simple cases, it has the side-effect of starting the scheduler multiple times when more than one TurboGears application instance is created inside the same Python Interpreter (which is what's happening when you run the test suite).

    I suggest you start the scheduler through a milestone, which are guaranteed to be run only once for each python interpreter ( http://turbogears.readthedocs.org/en/latest/turbogears/configuration/appconfig.html#configuration-milestones )

    Just edit your config/app_cfg.py and add following code at the end to start the scheduler:

    def start_tgscheduler():
        from tgscheduler import start_scheduler
        from tgscheduler.scheduler import add_interval_task
    
        start_scheduler()
    
        def testTask():
            print 'HELLO'
        add_interval_task(action=testTask, taskname="test1", interval=10, initialdelay=5)
    
    from tg.configuration import milestones
    milestones.config_ready.register(start_tgscheduler)
    

    That will properly ensure that the scheduler is started only once even when running the test suite.