简体   繁体   English

Pytest:如何设置我的 pytest 配置(conftest)以在测试环境中运行?

[英]Pytest : How to set on my pytest configuration(conftest) to run on a test environment?

I thought the easy way was to reload the config module with my software environment as 'Testing'.我认为最简单的方法是使用我的软件环境作为“测试”重新加载配置模块。

My code has a config.py which works as configuration handling code where I define all my configuration setting per each environment I'd like to run.我的代码有一个config.py ,它用作配置处理代码,我在其中定义每个我想运行的每个环境的所有配置设置。

config.py配置文件

class Config(object):
    DEBUG = False
    TESTING = False
    DATABASE_URI = 'sqlite:///:memory:'

class ProductionConfig(Config):
    DATABASE_URI = 'mysql://user@localhost/foo'

class DevelopmentConfig(Config):
    DEBUG = True

class TestingConfig(Config):
    TESTING = True

config_lookup = dict(
    testing=TestingConfig(),
    development=DevelopmentConfig(),
    production=ProductionConfig(),
)

config = config_lookup[os.getenv('ENVIRONMENT', 'development')]

One approach could be using reload: On my conftest.py I would like to make ensure that all my tests would run as per TestingConfig.一种方法是使用重新加载:在我的conftest.py ,我想确保我的所有测试都按照 TestingConfig 运行。 Therefore I would do something like this:因此我会做这样的事情:

conftest.py conftest.py

@pytest.fixture(autouse=True)
def cfg():
    # Reload the global 'config' instance.

    os.environ["ENVIRONMENT"] = "testing"
    importlib.reload(sys.modules["config"])

Unfortunately, this approach doesn't seem to work as expected for all situations as the importlib.reload() seems to perform expected.不幸的是,由于importlib.reload()似乎按预期执行,因此这种方法似乎无法在所有情况下都按预期工作。

importlib.reload() Reload a previously imported module. importlib.reload()重新加载之前导入的模块。 The argument must be a module object, so it must have been successfully imported before.参数必须是一个模块 object,所以它必须之前已经成功导入。 Read more: importlib Docs阅读更多: importlib 文档

This solution may work in the first instance but it will become tricky when using a different type of imports.(object vs submodule)此解决方案可能在第一种情况下有效,但在使用不同类型的导入时会变得很棘手。(对象与子模块)

An object is imported一个object是进口的

from app.config import config 

print('Environment is:',type(config).__name__)

# Environment is:Development <- Wrong

An module is imported导入了一个模块

import app.config as _ 

print('Environment is:',type(_.config).__name__)

# Environment is:Testing <-- correct

Another approach is to ensure you always set your environment variable when executing pytests: ENVIRONMENT=testing python -m pytest tests另一种方法是确保在执行 pytest 时始终设置环境变量: ENVIRONMENT=testing python -m pytest tests

Not a desirable as you might forget to append your environment.不是一个可取的,因为您可能会忘记 append 您的环境。

Related issues:相关问题:

difference-between-from-x-import-y-and-import-x-yPython Bug区别-from-x-import-y-and-import-x-yPython Bug

Change environment variables before importlib.reload 在 importlib.reload 之前更改环境变量

Reloading All Loaded Modules 重新加载所有加载的模块

difference-between-from-x-import-y-and-import-xy pep-0221 从-x-import-y-和-import-xy pep-0221之间的区别

Another approach that seems to work quite well is to ensure that you set your environment before running conftest.py .另一种似乎效果很好的方法是确保在运行conftest.py之前设置环境。 Is using python-env正在使用python-env

[pytest]
env =
    ENVIRONMENT=testing

Seems to work fine.似乎工作正常。 The upsidedown is that is an old repository without maintenance but is also a small piece of code.不利的是,这是一个没有维护的旧存储库,但也是一小段代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM