pythonflasknoseflask-script

Flask-Script command that passes all remaining args to nose?


I use a simple command to run my tests with nose:

@manager.command
def test():
    """Run unit tests."""
    import nose
    nose.main(argv=[''])

However, nose supports many useful arguments that now I could not pass.

Is there a way to run nose with manager command (similar to the call above) and still be able to pass arguments to nose? For example:

python manage.py test --nose-arg1 --nose-arg2

Right now I'd get an error from Manager that --nose-arg1 --nose-arg2 are not recognised as valid arguments. I want to pass those args as nose.main(argv= <<< args comming after python manage.py test ... >>>)


Solution

  • Flask-Script has an undocumented capture_all_flags flag, which will pass remaining args to the Command.run method. This is demonstrated in the tests.

    @manager.add_command
    class NoseCommand(Command):
        name = 'test'
        capture_all_args = True
    
        def run(self, remaining):
            nose.main(argv=remaining)
    
    python manage.py test --nose-arg1 --nose-arg2
    # will call nose.main(argv=['--nose-arg1', '--nose-arg2'])