Skip to content

Start using unittest for tests - #693

Merged
aclark4life merged 36 commits into
python-pillow:masterfrom
hugovk:unittest0
Jun 8, 2014
Merged

aclark4life merged 36 commits into
python-pillow:masterfrom
hugovk:unittest0

Conversation

@hugovk

@hugovk hugovk commented Jun 5, 2014

Copy link
Copy Markdown
Member

I spotted in PIL/tests.py the idea to start moving the tests to use the unittest module. Here's a start.

Notes:

  • Rather that putting them under PIL/ it's better they have their own directory. A common one is tests/ but we have the old ones in Tests/ so I'm putting them in test/ to make it clear which are which.
  • The old tester.py is now helper.py and contains helper functions like lena() and a base class PillowTestCase(unittest.TestCase).
  • unittest.TestCase has many assert functions for Python 2.7 onwards. For Python 2.6 we use unittest2 which has them backported.
  • PillowTestCase defines the extra asserts like assert_image_equal().
  • Individual test/test_*.py files have a test class that derives from PillowTestCase so they can use assert_image_equal() and so on.
  • Individual test/test_*.py have if __name__ == '__main__': unittest.main() so they can be individually run: python test/test_something.py
  • To run the whole batch (e.g. on Travis CI) it uses nosetests:
    • No coverage: nosetests test/
    • With coverage: coverage run --append --include=PIL/* -m nose test/
  • helper.py still has a lot of commented out stuff from tester.py. I'm uncommenting and updating things as needed, so no commented stuff will be left at the end.

TODO:

  • Convert the rest.
  • Implement tempfile() and cleanup.
    • The old tester deletes files at the end of test_something.py unless the test failed.
    • At the end of the whole test suite, it prints out any remaining temp files.

Any comments?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.47%) when pulling 34db40d on hugovk:unittest0 into 6858aef on python-pillow:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.11%) when pulling 34db40d on hugovk:unittest0 into 6858aef on python-pillow:master.

@hugovk

hugovk commented Jun 5, 2014

Copy link
Copy Markdown
Member Author

The reason coverage decreased 0.47% is because for some reason ImageGrab.py wasn't being included in the report with the old test runner. After, when being tested via unititest, it's included in the report.

Before coverage was 72.86%. After, when including ImageGrab.py, that file's 25% coverage brought the average down.

Before: https://coveralls.io/builds/835904
After: https://coveralls.io/builds/837087

(The reason for the second decrease (and failure) is an occasional Fatal RPython error with PyPy for test_imagedraw.py, meaning it's coverage didn't all get included. I've seen this problem with test_imagedraw.py on the old test runner too, so it's not directly related to using unittest.)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.11%) when pulling 07aa1a5 on hugovk:unittest0 into 6858aef on python-pillow:master.

hugovk added 7 commits June 5, 2014 16:13
Otherwise it sometimes, but not always, causes an error:

RPython traceback:
  File "rpython_jit_metainterp_compile.c", line 20472, in send_loop_to_backend
  File "rpython_jit_backend_x86_assembler.c", line 1818, in Assembler386_assemble_loop
  File "rpython_jit_backend_x86_regalloc.c", line 293, in RegAlloc_prepare_loop
  File "rpython_jit_backend_x86_regalloc.c", line 909, in RegAlloc__prepare
  File "rpython_jit_backend_llsupport_regalloc.c", line 4706, in compute_vars_longevity
Fatal RPython error: AssertionError
/home/travis/build.sh: line 236:  7300 Aborted
@hugovk

hugovk commented Jun 7, 2014

Copy link
Copy Markdown
Member Author

@aclark4life I'll merge it and update this PR tomorrow.

@aclark4life

Copy link
Copy Markdown
Member

Great, thanks

More tests and merge with upstream
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.12%) when pulling 0940f0b on hugovk:unittest0 into 8beb664 on python-pillow:master.

@hugovk

hugovk commented Jun 8, 2014

Copy link
Copy Markdown
Member Author

@aclark4life OK ready, more tests added and merged with upstream.

I'll add mores tests in another branch and create a new PR later. Still TODO:

  • Caching in lena().
  • Implement tempfile(). This will basically be the same as before.
  • Convert the rest of the tests.

But new tests, if tempfile() isn't needed, can be created based on unittest. If not, I can convert them later.

aclark4life added a commit that referenced this pull request Jun 8, 2014
Start using unittest for tests
@aclark4life
aclark4life merged commit 001b46c into python-pillow:master Jun 8, 2014
@aclark4life

Copy link
Copy Markdown
Member

Thanks!

@hugovk
hugovk deleted the unittest0 branch June 8, 2014 11:07
@wiredfool

Copy link
Copy Markdown
Member

So... Where's the test runner for these?

@wiredfool

Copy link
Copy Markdown
Member

Ok, found nose. Appears broken.

(vpy27)erics@builder-1204-x64:~/Pillow$ python setup.py install
running install
running bdist_egg
running egg_info
writing Pillow.egg-info/PKG-INFO

...

Installed /home/erics/vpy27/lib/python2.7/site-packages/Pillow-2.4.0-py2.7-linux-x86_64.egg
Processing dependencies for Pillow==2.4.0
Finished processing dependencies for Pillow==2.4.0

(vpy27)erics@builder-1204-x64:~/Pillow$ nosetests 2>&1 | more
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE...........EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE.ESEEEEESEEEEE
======================================================================
ERROR: Failure: ImportError (cannot import name _imaging)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/erics/vpy27/local/lib/python2.7/site-packages/nose/loader.py", line 414, in loadTestsFromName
    addr.filename, addr.module)
  File "/home/erics/vpy27/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/home/erics/vpy27/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/erics/Pillow/Tests/cms_test.py", line 7, in <module>
    from PIL import Image
  File "/home/erics/Pillow/PIL/Image.py", line 54, in <module>
    from PIL import _imaging as core
ImportError: cannot import name _imaging
...

@wiredfool

Copy link
Copy Markdown
Member

Also, now we've lost all the SCM context of the tests -- git blame and git log are essentially showing a bunch of brand new files.

@aclark4life

Copy link
Copy Markdown
Member

Try python setup.py test with b7f94e3

@aclark4life

Copy link
Copy Markdown
Member

@wiredfool How is it possible to lose context? Any idea what caused the loss of context? Maybe @hugovk can look into it…

@wiredfool

Copy link
Copy Markdown
Member

Ok, python setup.py test works.

I think that the context loss is basically due to the combination of moving the files and changing all of the lines at the same time due to hoisting them into classes.

I'm not good enough at git to know if there's a good solution, but it would be nice if there was a way to connect the history of the files together.

@aclark4life

Copy link
Copy Markdown
Member

Ah I see, that is annoying

@hugovk

hugovk commented Jun 10, 2014

Copy link
Copy Markdown
Member Author

To run the an individual test, use either:

  • python test/test_something.py
  • nosetests test/test_something.py

To run the whole batch (e.g. like on Travis CI) use nosetests:

  • No coverage: nosetests test/
  • With coverage: coverage run --append --include=PIL/* -m nose test/

All these can take a -v verbose flag at the end.

@hugovk

hugovk commented Jun 10, 2014

Copy link
Copy Markdown
Member Author

Re: history

I did git mv, update the test, then git add and git commit so I'd hoped history was preserved, but it appears not.

Git does have some rename support, and there's a git log --follow option, but that's not showing the history.

Here's an idea:

If you want to back up this PR out of master (there's been no unrelated commits afterwards) then when I have all the tests converted, I can make a fresh PR from a fresh branch by just replacing the files and leaving them in the Tests folder with no git mv.


Current status:

Branch: https://github.com/hugovk/Pillow/tree/unittest2tempfile

I'm almost done converting all the files, just have the last one to fix. test_cffi.py fails after conversion:

ERROR: test_get_vs_c (test_cffi.TestCffi)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/hugovk/Pillow/test/test_cffi.py", line 64, in test_get_vs_c
    self._test_get_access(lena('RGB'))
  File "/home/travis/build/hugovk/Pillow/test/test_cffi.py", line 55, in _test_get_access
    caccess = im.im.pixel_access(False)
AttributeError: 'NoneType' object has no attribute 'pixel_access'

It seems this is because the original lena() caches files and an earlier test modifies the image so it has an im.im. The converted test has caching disabled for now and always returns a freshly opened file (specifically to make sure tests are independent and repeatable) whose im.im is None.

(Well, there's one more I've not yet done -- test_001_archive.py -- but I'll remove it because it's going through a list of files in ../pil-archive/* and that directory doesn't exist.)

@aclark4life

Copy link
Copy Markdown
Member

How far back should I go, just: 001b46c or further?

@hugovk

hugovk commented Jun 10, 2014

Copy link
Copy Markdown
Member Author

@aclark4life To the one just before that, so 8beb664 will be the latest.

I've made a new branch based on 8beb664 and am adding the tests in with fresh commits so they remain in the Tests/ directory with preserved history.

@hugovk

hugovk commented Jun 10, 2014

Copy link
Copy Markdown
Member Author

@aclark4life

Copy link
Copy Markdown
Member

K, working on the revert(s)

aclark4life added a commit that referenced this pull request Jun 10, 2014
This reverts commit 001b46c, reversing
changes made to 8beb664.
@aclark4life

Copy link
Copy Markdown
Member

@hugovk Let me know how the revert looks and if OK I'll merge your new PR

@aclark4life

Copy link
Copy Markdown
Member

Looks like PyPy testing failed on Travis but we can probably fix that up

@hugovk hugovk mentioned this pull request Jun 10, 2014
1 of 5 tasks
@hugovk

hugovk commented Jun 10, 2014

Copy link
Copy Markdown
Member Author

@aclark4life PR here: #699

@aclark4life

Copy link
Copy Markdown
Member

@hugovk Thanks, so my revert looks OK?

@hugovk

hugovk commented Jun 10, 2014

Copy link
Copy Markdown
Member Author

@aclark4life Yes, all merged smoothly.

@aclark4life

Copy link
Copy Markdown
Member

Thanks, done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants