CleanCut / green

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

Can't run specific file containing tests #132

Closed mairsbw closed 8 years ago

mairsbw commented 8 years ago

I have a project with a test folder containing various test files that look like test_XXX.py. From the root directory of this project, running the entire test folder works fine:

$ python -m green test
....

But if I try to just specify a subfile using the . pathing syntax:

$ python -m green test.test_camera
Ran 0 tests in 0.109s

No Tests Found
$

And if I try to work around this by running the file directly:

$ python -m green test/test_camera.py
E

Error in .unittest.loader
TypeError: Test loader returned an un-runnable object.  Is "unittest.loader" importable from your current location?  Maybe you forgot an __init__.py in your directory?  Unrunnable object looks like: None of type <class 'NoneType'> with dir ['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Ran 1 test in 0.110s

FAILED (errors=1)

So I'm uncertain what the right solution here is. Note that I'm not in a library (there's no init.py folder in the root directory that I'm running these scripts from), so I don't know if I have things set up incorrectly or if this is a failing on green's part.

CleanCut commented 8 years ago

Is your code open source? If so, where is it? If not, can you provide a minimal example that I could reproduce locally?

mairsbw commented 8 years ago

It's not open source. Let me whip up an example today so you can reproduce.

mairsbw commented 8 years ago

It doesn't get much more minimal than this.

Reproduce by running in the base folder the commands:

$ python -m green test # This will work!
$ python -m green test.test_mytest # This will fail!
$ python -m green test/test_mytest.py # This will also fail!

green_test.zip

CleanCut commented 8 years ago

Oh, silly me. I should have caught that from your initial description. You're just missing an __init__.py in your test folder. See the readme section here for a discussion on that very thing.

mairsbw commented 8 years ago

Yeah, I was wondering if it was a misconfiguration on my part. Thanks for your help.

mairsbw commented 8 years ago

Although now I have the problem where the name has changed for my test modules, so they can't find a relative path where some test files are kept (like os.path.join(__name__, 'images')). Looks like instead of being named test_camera my test_camera.py script receives a name of test.test_camera. So this is wrong for the path. I could hardcode these, but I liked using the name variable so it was the same between all of my test files. Any suggestions here?

CleanCut commented 8 years ago

I'm afraid if you want to use green to run specific tests, the specific test modules need to be in a package. When modules are in a package, their name is going to reflect that.

In your particular example, you could adjust for it pretty simply:

os.path.join(__name__.split('.')[-1], 'images')
mairsbw commented 8 years ago

Got it, that makes sense. Thanks.