[PATCH 1 of 2 v4] tests: add as little code as possible in __init__.py

Thomas De Schampheleire patrickdepinguin at gmail.com
Mon Sep 19 19:34:28 UTC 2016


On Sep 19, 2016 9:30 PM, "Mads Kiilerich" <mads at kiilerich.com> wrote:
>
> On 09/19/2016 09:01 PM, Thomas De Schampheleire wrote:
>>
>> # HG changeset patch
>> # User Thomas De Schampheleire <thomas.de.schampheleire at gmail.com>
>> # Date 1474227861 -7200
>> #      Sun Sep 18 21:44:21 2016 +0200
>> # Node ID 7e0969f34a4cff5788f4815c7e66c441158666fc
>> # Parent  59639343672495cb89be54b98f1d6d3a4c44307c
>> tests: add as little code as possible in __init__.py
>>
>> kallithea/tests/__init__.py contains quite a lot of code, including the
test
>> base class TestController. This in itself may be considered bad practice.
>>
>> Specifically, this poses a problem when using pytest 3.0+, in which
asserts
>> in some files are not automatically rewritten to give improved assert
>> output. That problem can be fixed by explicitly registering such files
for
>> assertion rewriting, but that register call should be executed _before_
said
>> files are imported. I.e. if the register call is in
>> kallithea/tests/__init__.py, assert calls in __init__.py itself can not
be
>> rewritten.
>>
>> Since the TestController base class does effectively contain asserts,
and we
>> do not want to execute the register call from somewhere outside the
>> kallithea/tests directory, we need to move the TestController class to
>> another file (kallithea/tests/base.py). The import of that file then
needs
>> to happen _after_ executing the register call.
>>
>> While not strictly necessary to fix the mentioned pytest problem, we take
>> the opportunity to fully clean __init__.py and move everything possible
to
>> the new kallithea/tests/base.py. While doing so, unnecessary imports are
>> removed, and imports are ordered alphabetically. Explicit imports of
symbols
>> from modules that were already imported as a whole, are removed in favor
of
>> fully qualifying the references (e.g. tempfile._RandomNameSequence).
>
>
> Thanks
>
>
>> diff --git a/kallithea/tests/__init__.py b/kallithea/tests/__init__.py
>> --- a/kallithea/tests/__init__.py
>> +++ b/kallithea/tests/__init__.py
>> @@ -22,41 +22,10 @@ setup-app`) and provides the base testin
>>     Refer to docs/contributing.rst for details on running the test suite.
>>   """
>> -import os
>> -import re
>> -import time
>> -import logging
>> -import datetime
>> -import hashlib
>> -import tempfile
>>   -from tempfile import _RandomNameSequence
>> -
>> -import pylons
>> -import pylons.test
>> -from pylons import config, url
>> -from pylons.i18n.translation import _get_translator
>> -from pylons.util import ContextObj
>> -
>> -from routes.util import URLGenerator
>> -from webtest import TestApp
>>   import pytest
>>   -from kallithea.lib.compat import unittest
>> -from kallithea import is_windows
>> -from kallithea.model.db import Notification, User, UserNotification
>> -from kallithea.model.meta import Session
>> -from kallithea.lib.utils2 import safe_str
>> -
>> -
>> -os.environ['TZ'] = 'UTC'
>> -if not is_windows:
>> -    time.tzset()
>> -
>> -log = logging.getLogger(__name__)
>> -
>> -skipif = pytest.mark.skipif
>> -parametrize = pytest.mark.parametrize
>> +from kallithea.tests.base import *
>
>
> Would you have any objections to dropping this and just use
kallithea.tests.base where it is needed?
>
> Import * in general is one thing, but having it in a module definition
can make it even harder to figure out what is going on.

Does that work with the override of __all__? That is the real reason to
import this way. If there is a better way, yes please.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sfconservancy.org/pipermail/kallithea-general/attachments/20160919/d666c1d6/attachment.html>


More information about the kallithea-general mailing list