当尝试使用SQLAlchemy与db连接时,用pytest编写的测试代码不起作用

时间:2018-07-08 16:45:51

标签: python unit-testing flask sqlalchemy pytest

我正在用pytest编写测试代码,以测试用python 3编写的API。但是我在以下错误消息中苦苦挣扎。 python代码无需pytest即可正常工作,但仅在执行pytest代码时才会显示错误消息。我认为问题似乎并非来自低级问题。老实说,这个错误背后的真正含义对我来说毫无意义。这次上下文也似乎与此问题无关。 (http://flask-sqlalchemy.pocoo.org/contexts/

  

有人可以给我一些解决方案的提示吗?

*我已经尝试根据此问题('No application found. Either work inside a view function or push an application context.')解决问题,但不适用于此代码。

可能与问题相关的部分代码。 (test_auth.py)

    app = Flask(__name__)
    app.config.from_object(test)
    db_acs.init_app(app)
    logger = logging.getLogger(__name__)
    init_app(app, db_acs, logger)

错误

=========================================================================================== FAILURES ============================================================================================
____________________________________________________________________________________ test______________________________________________________________________________________

    def test():
        sqlpath = app.config['SQL_PATH']['getid']
        input_id = 'xxx'

>       rows = db_acs.dba(sqlpath, s1=input_id)

test_auth.py:68: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../xxx/xxx/db/dba.py:33: in dba
    connection = db.engine.connect()
../../../.pyenv/versions/3.6.5/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:877: in engine
    return self.get_engine()
../../../.pyenv/versions/3.6.5/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:886: in get_engine
    app = self.get_app(app)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <SQLAlchemy engine=None>, reference_app = None

    def get_app(self, reference_app=None):
        """Helper method that implements the logic to look up an
            application."""

        if reference_app is not None:
            return reference_app

        if current_app:
            return current_app._get_current_object()

        if self.app is not None:
            return self.app

        raise RuntimeError(
>           'No application found. Either work inside a view function or push'
            ' an application context. See'
            ' http://flask-sqlalchemy.pocoo.org/contexts/.'
        )
E       RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

1 个答案:

答案 0 :(得分:0)

在文件import matplotlib as mpl from matplotlib.text import TextPath from matplotlib.patches import PathPatch from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt import itertools import operator import numpy as np fp = FontProperties(family="Arial", weight="bold") globscale = 1.35 LETTERS = { "T" : TextPath((-0.305, 0), "T", size=1, prop=fp), "G" : TextPath((-0.384, 0), "G", size=1, prop=fp), "A" : TextPath((-0.35, 0), "A", size=1, prop=fp), "C" : TextPath((-0.366, 0), "C", size=1, prop=fp) } COLOR_SCHEME = {'G': 'gold', 'A': 'forestgreen', 'C': 'mediumblue', 'T': 'crimson'} def letterAt(letter, x, y, yscale=1, ax=None): text = LETTERS[letter] t = mpl.transforms.Affine2D().scale(1*globscale, yscale*globscale) + \ mpl.transforms.Affine2D().translate(x,y) + ax.transData p = PathPatch(text, lw=0, fc=COLOR_SCHEME[letter], transform=t) if ax != None: ax.add_artist(p) return p ALL_SCORES1 = [[('C', 0.02247014831444764), ('T', 0.057903843733384308), ('A', 0.10370837683591219), ('G', 0.24803586793255664)], [('T', 0.046608227674354567), ('G', 0.048827667087419063), ('A', 0.084338697696451109), ('C', 0.92994511407402669)], [('G', 0.0), ('T', 0.011098351287382456), ('A', 0.022196702574764911), ('C', 1.8164301607015951)], [('C', 0.020803153636453006), ('T', 0.078011826136698756), ('G', 0.11268374886412044), ('A', 0.65529933954826969)], [('T', 0.017393530660176126), ('A', 0.030438678655308221), ('G', 0.22611589858228964), ('C', 0.45078233627623127)], [('G', 0.022364103549245576), ('A', 0.043412671595594352), ('T', 0.097349627214363091), ('C', 0.1657574733649966)], [('C', 0.03264675899941203), ('T', 0.045203204768416654), ('G', 0.082872542075430544), ('A', 1.0949220710572034)], [('C', 0.0), ('T', 0.0076232429756614498), ('A', 0.011434864463492175), ('G', 1.8867526364762088)], [('C', 0.0018955903000026028), ('T', 0.0094779515000130137), ('A', 0.35637097640048931), ('G', 0.58005063180079641)], [('A', 0.01594690817903021), ('C', 0.017541598996933229), ('T', 0.2774762023151256), ('G', 0.48638069946042134)], [('A', 0.003770051401807444), ('C', 0.0075401028036148881), ('T', 0.011310154205422331), ('G', 1.8624053924928772)], [('C', 0.036479877757360731), ('A', 0.041691288865555121), ('T', 0.072959755514721461), ('G', 1.1517218549109602)], [('G', 0.011831087684038642), ('T', 0.068620308567424126), ('A', 0.10174735408273231), ('C', 1.0009100180696691)], [('C', 0.015871770937774379), ('T', 0.018757547471915176), ('A', 0.32176408355669878), ('G', 0.36505073156881074)], [('A', 0.022798100897300954), ('T', 0.024064662058262118), ('G', 0.24571286522646588), ('C', 0.34070495229855319)]] fig, ax = plt.subplots(figsize=(10,3)) all_scores = ALL_SCORES1 x = 1 maxi = 2 for scores in all_scores: y = 0 for base, score in scores: letterAt(base, x,y, -score, ax) y -= score x += 1 maxi = max(maxi, y) plt.xticks(range(1,x)) plt.xlim((0, x)) plt.ylim((-2, maxi)) plt.tight_layout() plt.show() 中,您可以这样写:

conftest.py

在您的test_function中:

@pytest.fixture(scope="session")
def app():
    app = create_app()  # Or app = Flask(__name__)
    return app