CleanCut / green

Green is a clean, colorful, fast python test runner.
MIT License
786 stars 74 forks source link

Custom runner script #145

Closed agb80 closed 7 years ago

agb80 commented 7 years ago

I would like to use green for test an app we develop on python. The problem I'm facing now is that for run the test we need open a database.

At this time I made a little change on the green script to open database session before run the test

import re
import sys

from green import main

DATABASE_NAME = 'internet'
session.open(db=DATABASE_NAME)

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

The problem with this method is that database name is hardcoded. Could you suggest a way that I can create a custom runner script with a new param for indicate database name to open?

Thanks in advance!

CleanCut commented 7 years ago

Write initializer and finalizer functions. These functions will be run by green inside each worker process before any tests are run and after all tests finish.

If you only have a single, shared database that can't have multiple tests run at the same time, make sure to specify a single process. (--processes 1)

For example, if you made a function named connectToDB and put it in myfile.py you could do

green -i myfile.connectToDB stuff_to_test
CleanCut commented 7 years ago

Here's what green --help has to say about the initializer and finalizer:

  -i DOTTED_FUNCTION, --initializer DOTTED_FUNCTION
                        Python function to run inside of a single worker
                        process before it starts running tests. This is the
                        way to provision external resources that each
                        concurrent worker process needs to have exclusive
                        access to. Specify the function in dotted notation in
                        a way that will be importable from the location you
                        are running green from.
  -z DOTTED_FUNCTION, --finalizer DOTTED_FUNCTION
                        Same as --initializer, only run at the end of a worker
                        process's lifetime. Used to unprovision resources
                        provisioned by the initializer.
agb80 commented 7 years ago

The initializer and finalizer are a good option, but still don't get on how can I add a param on the command to call green. I'm thinking something like this:

green -i myfile.connectToDB -db mydb stuff_to_test
CleanCut commented 7 years ago

Oh, I see what you're asking. Well, if that's all you want to do, just modify your little script to do it.

import re
import sys

from green import main

session.open(db=sys.argv[1])
del(sys.argv[1])

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())
agb80 commented 7 years ago

Thank you very much, works pretty well.