Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
配置wercker.yml以在需要postgres服务的python应用程序中运行单元测试_Python_Unit Testing_Docker_Wercker - Fatal编程技术网

配置wercker.yml以在需要postgres服务的python应用程序中运行单元测试

配置wercker.yml以在需要postgres服务的python应用程序中运行单元测试,python,unit-testing,docker,wercker,Python,Unit Testing,Docker,Wercker,我试图使用wercker的Docked版本在python应用程序中运行单元测试,但我的大多数单元测试都失败了,出现错误sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:没有到主机的路由。wercker文档中缺少进行此连接的配置 也许我遗漏了一些简单的东西,但在花费了比我应该拥有的更多的时间之后,我无法建立这个db连接来运行我的测试。无论如何,Rails应用程序也有类似的问题,但答案没有帮助,所以这个问题可能会帮

我试图使用wercker的Docked版本在python应用程序中运行单元测试,但我的大多数单元测试都失败了,出现错误
sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:没有到主机的路由。wercker文档中缺少进行此连接的配置

也许我遗漏了一些简单的东西,但在花费了比我应该拥有的更多的时间之后,我无法建立这个db连接来运行我的测试。无论如何,Rails应用程序也有类似的问题,但答案没有帮助,所以这个问题可能会帮助其他人使用python应用程序。寻求帮助

以下是我当前的wercker.yml:

# http://devcenter.wercker.com/docs/containers/index.html

box: python:3.4.4-onbuild

# You can also use services such as databases. Read more on our dev center:
# http://devcenter.wercker.com/docs/services/index.html

services:
    # http://devcenter.wercker.com/docs/services/postgresql.html
    - id: postgres:9.4
      env:
        POSTGRES_PASSWORD: mylittlesecret
        POSTGRES_USER: postgres  # optional

# This is the build pipeline. Pipelines are the core of wercker
# Read more about pipelines on our dev center
# http://devcenter.wercker.com/docs/pipelines/index.html

build:
  # The steps that will be executed on build
  # Steps make up the actions in your pipeline
  # Read more about steps on our dev center:
  # http://devcenter.wercker.com/docs/steps/index.html
  steps:
    # A step that sets up the python virtual environment
    - script:
        name: virtualenv install
        code: |
          pip install virtualenv

    - virtualenv:
        name: venv

    - pip-install:
        requirements_file: "requirements.txt"

    # A custom script step, name value is used in the UI
    # and the code value contains the command that get executed
    - script:
        name: echo python information
        code: |
          echo "python version $(python --version) running"
          echo "pip version $(pip --version) running"



    # A step that loads the database schema in order to run the tests - perhaps not needed!!
    - script:
        name: Set up db
        code: |
          echo "Start postgresql server ..."
          echo "Create test db ..."
          echo "Created test db ..."

    # Debug lines I've used in above script
    # export PATH=/usr/pgsql-9.4.6/bin:$PATH  ==> no impact
    # export PATH=/usr/lib/postgresql/9.4/bin:$PATH  ==> no impact
    # psql -c "CREATE DATABASE scrdbtest;"  ==> ERROR: psql command not found
    # postgres -D /var/lib/postgresql/data ==> ERROR: postgres not found

    - script:
        name: run aoscrdb tests
        code: |
          /root/venv/bin/python -m pytest ./tests
这是我的应用程序测试配置:

class TestConfig(Config):
    TESTING = True
    DEBUG = True
    # wercker
    SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://postgres:mylittlesecret@{}:5432/postgres'\
.format(os.environ.get('POSTGRES_PORT_5432_TCP_ADDR'))
最后,这里的一个问题是wercker的失败测试:

 ============================= test session starts ==============================
platform linux -- Python 3.4.4, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /pipeline/source/tests, inifile: 
collected 39 items

tests/test_config.py ..
tests/test_forms.py EEEEEEEEEEEEEEE
tests/test_functional.py EEEEEEE
tests/test_models.py EEEEEEEEEEEEEEE

==================================== ERRORS ====================================
___ ERROR at setup of TestRegisterForm.test_validate_user_already_registered ___

app = <Flask 'aoscrdb_app.app'>

    @pytest.yield_fixture(scope='function')
    def db(app):
        """A database for the tests."""
        _db.app = app
        with app.app_context():
>           _db.create_all()

tests/conftest.py:36: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/root/venv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py:972: in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
/root/venv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py:964: in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
/root/venv/lib/python3.4/site-packages/sqlalchemy/sql/schema.py:3695: in create_all
    tables=tables)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:1855: in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
/usr/local/lib/python3.4/contextlib.py:59: in __enter__
    return next(self.gen)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:1848: in _optional_conn_ctx_manager
    with self.contextual_connect() as conn:
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:2039: in contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:2078: in _wrap_pool_connect
    e, dialect, self)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:1405: in _handle_dbapi_exception_noconnection
    exc_info
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/compat.py:189: in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=exc_value)
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/compat.py:182: in reraise
    raise value.with_traceback(tb)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:2074: in _wrap_pool_connect
    return fn()
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:376: in connect
    return _ConnectionFairy._checkout(self)
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:713: in _checkout
    fairy = _ConnectionRecord.checkout(pool)
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:480: in checkout
    rec = pool._do_get()
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:1060: in _do_get
    self._dec_overflow()
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py:60: in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/compat.py:183: in reraise
    raise value
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:1057: in _do_get
    return self._create_connection()
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:323: in _create_connection
    return _ConnectionRecord(self)
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:449: in __init__
    self.connection = self.__connect()
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:607: in __connect
    connection = self.__pool._invoke_creator(self)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/strategies.py:97: in connect
    return dialect.connect(*cargs, **cparams)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/default.py:385: in connect
    return self.dbapi.connect(*cargs, **cparams)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

dsn = 'dbname=postgres user=postgres password=mysecretpassword host=172.17.0.10'
database = 'postgres', user = 'postgres', password = 'mysecretpassword'
host = '172.17.0.10', port = None, connection_factory = None
cursor_factory = None, async = False, kwargs = {}
items = [('dbname', 'postgres'), ('user', 'postgres'), ('password', 'mysecretpassword'), ('host', '172.17.0.10')]

    def connect(dsn=None,
            database=None, user=None, password=None, host=None, port=None,
            connection_factory=None, cursor_factory=None, async=False, **kwargs):
        """
        Create a new database connection.

        The connection parameters can be specified either as a string:

            conn = psycopg2.connect("dbname=test user=postgres password=secret")

        or using a set of keyword arguments:

            conn = psycopg2.connect(database="test", user="postgres", password="secret")

        The basic connection parameters are:

        - *dbname*: the database name (only in dsn string)
        - *database*: the database name (only as keyword argument)
        - *user*: user name used to authenticate
        - *password*: password used to authenticate
        - *host*: database host address (defaults to UNIX socket if not provided)
        - *port*: connection port number (defaults to 5432 if not provided)

        Using the *connection_factory* parameter a different class or connections
        factory can be specified. It should be a callable object taking a dsn
        argument.

        Using the *cursor_factory* parameter, a new default cursor factory will be
        used by cursor().

        Using *async*=True an asynchronous connection will be created.

        Any other keyword parameter will be passed to the underlying client
        library: the list of supported parameters depends on the library version.

        """
        items = []
        if database is not None:
            items.append(('dbname', database))
        if user is not None:
            items.append(('user', user))
        if password is not None:
            items.append(('password', password))
        if host is not None:
            items.append(('host', host))
        if port is not None:
            items.append(('port', port))

        items.extend([(k, v) for (k, v) in kwargs.items() if v is not None])

        if dsn is not None and items:
            raise TypeError(
                "'%s' is an invalid keyword argument when the dsn is specified"
                    % items[0][0])

        if dsn is None:
            if not items:
                raise TypeError('missing dsn and no parameters')
            else:
                dsn = " ".join(["%s=%s" % (k, _param_escape(str(v)))
                    for (k, v) in items])

>       conn = _connect(dsn, connection_factory=connection_factory, async=async)
E       sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: No route to host
E           Is the server running on host "172.17.0.10" and accepting
E           TCP/IP connections on port 5432?
=================================================测试会话开始==============================
平台linux——Python 3.4.4、pytest-2.8.7、py-1.4.31、Plugy-0.3.1
rootdir:/pipeline/source/tests,文件:
收集了39件物品
测试/test_config.py。。
测试/测试表格
测试/测试_functional.py eee
测试/测试_models.py eeee
===================================================错误====================================
___设置TestRegisterPerform.test\u validate\u user\u已注册时出错___
应用=
@pytest.yield_夹具(scope='function')
def数据库(应用程序):
“”“用于测试的数据库。”“”
_db.app=app
使用app.app_context():
>_db.create_all()
测试/确认测试。py:36:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/root/venv/lib/python3.4/site-packages/flask\u-sqlalchemy/\u_-init\u uuuuuuuu.py:972:in-create\u-all
self.\u为所有表执行(应用程序、绑定、“创建所有表”)
/root/venv/lib/python3.4/site packages/flask\u sqlalchemy/\uuuuuu init\uuuuuu.py:964:in\u execute\u for\u all\u表
op(绑定=自身获取引擎(应用,绑定),**额外)
/root/venv/lib/python3.4/site-packages/sqlalchemy/sql/schema.py:3695:in-create\u-all
表=表)
/root/venv/lib/python3.4/site packages/sqlalchemy/engine/base.py:1855:in\u run\u visitor
使用self.\u可选\u连接\u ctx\u管理器(连接)作为连接:
/usr/local/lib/python3.4/contextlib.py:59:in\uu输入__
返回下一个(self.gen)
/root/venv/lib/python3.4/site packages/sqlalchemy/engine/base.py:1848:in\u可选\u conn\u ctx\u管理器
使用self.context_connect()作为连接:
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:2039:在上下文连接中
self.\u wrap\u pool\u connect(self.pool.connect,无),
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:2078:in\u-wrap\u-pool\u-connect
e、 方言(自我)
/root/venv/lib/python3.4/site packages/sqlalchemy/engine/base.py:1405:in\u handle\u dbapi\u exception\u noconnection
exc_信息
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/compat.py:189:in-raise\u from\u cause
重新释放(类型(异常)、异常、tb=exc\U tb、原因=exc\U值)
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/compat.py:182:in-reraise
通过_回溯(tb)提升值
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/base.py:2074:in\u-wrap\u-pool\u-connect
返回fn()
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:376:in-connect
返回\u连接\u签出(自我)
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:713:in\u签出
fairy=\u ConnectionRecord.checkout(池)
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:480:签出中
rec=pool.\u do\u get()
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:1060:in\u-do\u-get
self._dec_overflow()
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py:60:in\uu退出__
兼容性(exc_类型、exc_值、exc_tb)
/root/venv/lib/python3.4/site-packages/sqlalchemy/util/compat.py:183:in-reraise
增值
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:1057:in\u-do\u-get
返回self.\u创建\u连接()
/root/venv/lib/python3.4/site packages/sqlalchemy/pool.py:323:in\u创建\u连接
返回连接记录(自身)
/root/venv/lib/python3.4/site packages/sqlalchemy/pool.py:449:in\uu init__
self.connection=self.\u connect()
/root/venv/lib/python3.4/site-packages/sqlalchemy/pool.py:607:in\uu-connect
连接=self.\u池.\u调用\u创建者(self)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/strategies.py:97:in-connect
返回方言。连接(*cargs,**cparams)
/root/venv/lib/python3.4/site-packages/sqlalchemy/engine/default.py:385:in-connect
返回self.dbapi.connect(*cargs,**cparams)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
dsn='dbname=postgres user=postgres password=mysecretpassword主机=172.17.0.10'
数据库='postgres',用户='postgres',密码='mysecretpassword'
主机='172.17.0.10',端口=无,连接工厂=无
游标\工厂=None,异步=False,kwargs={}
items=[('dbname','postgres'),('user','postgres'),('password','mysecretpassword'),('host','172.17.0.10')]
def连接(dsn=无,
数据库=无,用户=无,密码=无,主机=无,端口=无,
连接\u工厂=无,光标\u工厂=无,异步=假,**kwargs):
"""
创建新的数据库连接。
可以将连接参数指定为字符串:
conn=psycopg2.connect(“dbname=test user=postgres password=secret”)
或者使用一组关键字参数:
conn=psycopg2.connect(database=“test”,user=“postgres”,password=“secret”)
基本连接参数为:
-*dbname*:数据库名称(仅在dsn字符串中)
-*数据库*:数据库名称(仅作为关键字参数)
-*user*:用于身份验证的用户名
-*密码*:用于身份验证的密码
-*主机*:数据库主机地址(如果未提供,则默认为UNIX套接字)
-*端口*:连接