2

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 ... >>>)

4

3 に答える 3

4

あなたのソースでは、実行されたものがどこにも文書化されていない属性が設定されている場合、エラーが防止されるflask_scriptことがわかります。"too many arguments"Commandcapture_all_argsTrue

マネージャーを実行する直前に、クラスにその属性を設定できます

if __name__ == "__main__":
    from flask.ext.script import Command
    Command.capture_all_args = True
    manager.run()

このように、マネージャーへの追加の引数は常に受け入れられます。

このクイック フィックスの欠点は、オプションや引数を通常の方法でコマンドに登録できないことです。

それでもその機能が必要な場合は、このようにデコレータをサブクラス化しManagerてオーバーライドできますcommand

class MyManager(Manager):
    def command(self, capture_all=False):
        def decorator(func):
            command = Command(func)
            command.capture_all_args = capture_all
            self.add_command(func.__name__, command)

            return func
        return decorator

command次に、このようにデコレータを使用できます

@manager.command(True)  # capture all arguments
def use_all(*args):
    print("args:", args[0])

@manager.command()  # normal way of registering arguments
def normal(name):
    print("name", name)

何らかの理由で varargs を受け入れるflask_script必要がありますが、少し奇妙な引数のリストを格納することに注意してください。動作せず、失敗しますuse_allargs[0]def use_all(args):TypeError "got multiple values for argument 'args'"

于 2016-02-10T14:51:52.803 に答える
0

ダビディズムのソルンで、引数の一部しか受信されないという問題が発生しました

ドキュメントをもう少し調べると、nose.main が自動的に stdin を取得することが文書化されています

http://nose.readthedocs.io/en/latest/api/core.html

したがって、今は次のものを使用しています。

@manager.add_command
class NoseCommand(Command):
    name = 'nose'
    capture_all_args = True

    def run(self, remaining):
        nose.main()
于 2016-11-22T21:11:23.153 に答える