acoomans / flask-autodoc

Flask autodoc automatically creates an online documentation for your flask application.
MIT License
98 stars 49 forks source link

Unittests cannot be run more than once without cleaning .pyc files #22

Closed BallisticBuddha closed 9 years ago

BallisticBuddha commented 9 years ago

This is mainly an issue for local development by running the unittests locally. Found with Python 2.7.6

To reproduce:

This results in the following error:

======================================================================
FAIL: testGet (tests.test_autodoc.TestAutodoc)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/alex/git/flask-autodoc/tests/test_autodoc.py", line 33, in testGet
    self.assertIn(__file__, d['location']['filename'])
AssertionError: '/home/alex/git/flask-autodoc/tests/test_autodoc.pyc' not found in '/home/alex/git/flask-autodoc/tests/test_autodoc.py'

======================================================================
FAIL: testPost (tests.test_autodoc.TestAutodoc)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/alex/git/flask-autodoc/tests/test_autodoc.py", line 53, in testPost
    self.assertIn(__file__, d['location']['filename'])
AssertionError: '/home/alex/git/flask-autodoc/tests/test_autodoc.pyc' not found in '/home/alex/git/flask-autodoc/tests/test_autodoc.py'

This is due to the tests that verify the location property being run from the compiled .pyc bytecode, therefore making the source of the actual file running the test the .pyc file, which appears as the __file__ attribute in this scenario.

I can see three possible solutions to this:

lukeyeager commented 9 years ago

Thanks for the detailed bug report. I actually ran into this myself while testing #19. Here's how I fixed it in that pull request - what do you think?

    @staticmethod
    def thisFile():
        """Returns the basename of __file__ without a trailing 'c'"""
        filename = os.path.basename(__file__)
        if filename.endswith('.pyc'):
            filename = filename[:-1]
        return filename

#...

        self.assertIn(self.thisFile(), d['location']['filename'])
BallisticBuddha commented 9 years ago

Yep, that looks good, and is what I was talking about with my first suggestion by ignoring the last character in this instance.

lukeyeager commented 9 years ago

Closed in #19.