<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">I was trying to write unit tests for my flask restful api which uses BasicHttpAuth on the initial request and returns a JWT token. All subsequent requests have this token in an x-access-header up to an expiry of 15 mins, then re-authentication happens( the code for this is in the anacropapi repo on my profile under api.<strong>init</strong>.py). I have noted the error in the template. I thought this test client would be fully compatible with the requests package. Does anyone know of a work around for this or can an auth keyword arg be added ?</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import unittest 

class ApiTest(unittest.TestCase): 

    def setUp(self):
        # creates a test client
        self.app = app.test_client()
        # add a user into the account code goes here ...

    def tearDown(self):
        db.drop_all()
        # etc...

    def test_login(self):
        # sends HTTP GET request to the application
        # on the specified path with an auth kwarg passed like in requests 
         result = self.app.get('/login', auth=requests.auth.BasicHttpAuth(username, password)) "><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">unittest</span> 

<span class="pl-k">class</span> <span class="pl-v">ApiTest</span>(<span class="pl-s1">unittest</span>.<span class="pl-v">TestCase</span>): 

    <span class="pl-k">def</span> <span class="pl-en">setUp</span>(<span class="pl-s1">self</span>):
        <span class="pl-c"># creates a test client</span>
        <span class="pl-s1">self</span>.<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-s1">app</span>.<span class="pl-en">test_client</span>()
        <span class="pl-c"># add a user into the account code goes here ...</span>

    <span class="pl-k">def</span> <span class="pl-en">tearDown</span>(<span class="pl-s1">self</span>):
        <span class="pl-s1">db</span>.<span class="pl-en">drop_all</span>()
        <span class="pl-c"># etc...</span>

    <span class="pl-k">def</span> <span class="pl-en">test_login</span>(<span class="pl-s1">self</span>):
        <span class="pl-c"># sends HTTP GET request to the application</span>
        <span class="pl-c"># on the specified path with an auth kwarg passed like in requests </span>
         <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-s1">self</span>.<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">'/login'</span>, <span class="pl-s1">auth</span><span class="pl-c1">=</span><span class="pl-s1">requests</span>.<span class="pl-s1">auth</span>.<span class="pl-v">BasicHttpAuth</span>(<span class="pl-s1">username</span>, <span class="pl-s1">password</span>)) </pre></div>
<h3 dir="auto">Actual Behavior</h3>
<div class="highlight highlight-text-python-traceback notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="this returns no auth arg found error i cant remember the specfic error off hand "><pre class="notranslate">this returns no auth arg found error i cant remember the specfic error off hand </pre></div>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Python version: 3.6</li>
<li>Flask version: latest</li>
<li>Werkzeug version: latest</li>
</ul>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">According to <a href="https://flask.palletsprojects.com/en/master/testing/#the-first-test" rel="nofollow">https://flask.palletsprojects.com/en/master/testing/#the-first-test</a>, test should pass</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import os
import tempfile

import pytest

from flaskr import create_app


@pytest.fixture
def client():
    db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
    flaskr.app.config['TESTING'] = True

    with flaskr.app.test_client() as client:
        with flaskr.app.app_context():
            flaskr.init_db()
        yield client

    os.close(db_fd)
    os.unlink(flaskr.app.config['DATABASE'])

def test_empty_db(client):
    &quot;&quot;&quot;Start with a blank database.&quot;&quot;&quot;

    rv = client.get('/')
    assert b'No entries here so far' in rv.data"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">os</span>
<span class="pl-k">import</span> <span class="pl-s1">tempfile</span>

<span class="pl-k">import</span> <span class="pl-s1">pytest</span>

<span class="pl-k">from</span> <span class="pl-s1">flaskr</span> <span class="pl-k">import</span> <span class="pl-s1">create_app</span>


<span class="pl-en">@<span class="pl-s1">pytest</span>.<span class="pl-s1">fixture</span></span>
<span class="pl-k">def</span> <span class="pl-en">client</span>():
    <span class="pl-s1">db_fd</span>, <span class="pl-s1">flaskr</span>.<span class="pl-s1">app</span>.<span class="pl-s1">config</span>[<span class="pl-s">'DATABASE'</span>] <span class="pl-c1">=</span> <span class="pl-s1">tempfile</span>.<span class="pl-en">mkstemp</span>()
    <span class="pl-s1">flaskr</span>.<span class="pl-s1">app</span>.<span class="pl-s1">config</span>[<span class="pl-s">'TESTING'</span>] <span class="pl-c1">=</span> <span class="pl-c1">True</span>

    <span class="pl-k">with</span> <span class="pl-s1">flaskr</span>.<span class="pl-s1">app</span>.<span class="pl-en">test_client</span>() <span class="pl-k">as</span> <span class="pl-s1">client</span>:
        <span class="pl-k">with</span> <span class="pl-s1">flaskr</span>.<span class="pl-s1">app</span>.<span class="pl-en">app_context</span>():
            <span class="pl-s1">flaskr</span>.<span class="pl-en">init_db</span>()
        <span class="pl-k">yield</span> <span class="pl-s1">client</span>

    <span class="pl-s1">os</span>.<span class="pl-en">close</span>(<span class="pl-s1">db_fd</span>)
    <span class="pl-s1">os</span>.<span class="pl-en">unlink</span>(<span class="pl-s1">flaskr</span>.<span class="pl-s1">app</span>.<span class="pl-s1">config</span>[<span class="pl-s">'DATABASE'</span>])

<span class="pl-k">def</span> <span class="pl-en">test_empty_db</span>(<span class="pl-s1">client</span>):
    <span class="pl-s">"""Start with a blank database."""</span>

    <span class="pl-s1">rv</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-en">get</span>(<span class="pl-s">'/'</span>)
    <span class="pl-k">assert</span> <span class="pl-s">b'No entries here so far'</span> <span class="pl-c1">in</span> <span class="pl-s1">rv</span>.<span class="pl-s1">data</span></pre></div>
<h3 dir="auto">Actual Behavior</h3>
<p dir="auto">The test fail</p>
<div class="highlight highlight-text-python-traceback notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="(test2) florian@POC-Docker-1:~/test_testing_flask/flask/examples/tutorial$ pytest -v
========================================================================================================== test session starts ==========================================================================================================
platform linux -- Python 3.6.9, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 -- /home/florian/.pyenv/versions/3.6.9/envs/test2/bin/python3.6
cachedir: .pytest_cache
rootdir: /home/florian/test_testing_flask/flask/examples/tutorial, configfile: setup.cfg, testpaths: tests
collected 25 items                                                                                                                                                                                                                      

tests/test_auth.py::test_register PASSED                                                                                                                                                                                          [  4%]
tests/test_auth.py::test_register_validate_input[--Username is required.] PASSED                                                                                                                                                  [  8%]
tests/test_auth.py::test_register_validate_input[a--Password is required.] PASSED                                                                                                                                                 [ 12%]
tests/test_auth.py::test_register_validate_input[test-test-already registered] PASSED                                                                                                                                             [ 16%]
tests/test_auth.py::test_login PASSED                                                                                                                                                                                             [ 20%]
tests/test_auth.py::test_login_validate_input[a-test-Incorrect username.] PASSED                                                                                                                                                  [ 24%]
tests/test_auth.py::test_login_validate_input[test-a-Incorrect password.] PASSED                                                                                                                                                  [ 28%]
tests/test_auth.py::test_logout PASSED                                                                                                                                                                                            [ 32%]
tests/test_blog.py::test_index PASSED                                                                                                                                                                                             [ 36%]
tests/test_blog.py::test_login_required[/create] PASSED                                                                                                                                                                           [ 40%]
tests/test_blog.py::test_login_required[/1/update] PASSED                                                                                                                                                                         [ 44%]
tests/test_blog.py::test_login_required[/1/delete] PASSED                                                                                                                                                                         [ 48%]
tests/test_blog.py::test_author_required PASSED                                                                                                                                                                                   [ 52%]
tests/test_blog.py::test_exists_required[/2/update] PASSED                                                                                                                                                                        [ 56%]
tests/test_blog.py::test_exists_required[/2/delete] PASSED                                                                                                                                                                        [ 60%]
tests/test_blog.py::test_create PASSED                                                                                                                                                                                            [ 64%]
tests/test_blog.py::test_update PASSED                                                                                                                                                                                            [ 68%]
tests/test_blog.py::test_create_update_validate[/create] PASSED                                                                                                                                                                   [ 72%]
tests/test_blog.py::test_create_update_validate[/1/update] PASSED                                                                                                                                                                 [ 76%]
tests/test_blog.py::test_delete PASSED                                                                                                                                                                                            [ 80%]
tests/test_db.py::test_get_close_db PASSED                                                                                                                                                                                        [ 84%]
tests/test_db.py::test_init_db_command PASSED                                                                                                                                                                                     [ 88%]
tests/test_factory.py::test_config PASSED                                                                                                                                                                                         [ 92%]
tests/test_factory.py::test_hello PASSED                                                                                                                                                                                          [ 96%]
tests/test_flaskr.py::test_empty_db ERROR                                                                                                                                                                                         [100%]

================================================================================================================ ERRORS =================================================================================================================
____________________________________________________________________________________________________ ERROR at setup of test_empty_db ____________________________________________________________________________________________________

    @pytest.fixture
    def client():
&gt;       db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
E       NameError: name 'flaskr' is not defined

tests/test_flaskr.py:11: NameError
======================================================================================================== short test summary info ========================================================================================================
ERROR tests/test_flaskr.py::test_empty_db - NameError: name 'flaskr' is not defined
====================================================================================================== 24 passed, 1 error in 1.30s ======================================================================================================"><pre class="notranslate">(test2) florian@POC-Docker-1:~/test_testing_flask/flask/examples/tutorial$ pytest -v
========================================================================================================== test session starts ==========================================================================================================
platform linux -- Python 3.6.9, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 -- /home/florian/.pyenv/versions/3.6.9/envs/test2/bin/python3.6
<span class="pl-en">cachedir</span>: <span class="pl-s">.pytest_cache</span>
<span class="pl-en">rootdir</span>: <span class="pl-s">/home/florian/test_testing_flask/flask/examples/tutorial, configfile: setup.cfg, testpaths: tests</span>
collected 25 items                                                                                                                                                                                                                      

tests/test_auth.py::test_register PASSED                                                                                                                                                                                          [  4%]
tests/test_auth.py::test_register_validate_input[--Username is required.] PASSED                                                                                                                                                  [  8%]
tests/test_auth.py::test_register_validate_input[a--Password is required.] PASSED                                                                                                                                                 [ 12%]
tests/test_auth.py::test_register_validate_input[test-test-already registered] PASSED                                                                                                                                             [ 16%]
tests/test_auth.py::test_login PASSED                                                                                                                                                                                             [ 20%]
tests/test_auth.py::test_login_validate_input[a-test-Incorrect username.] PASSED                                                                                                                                                  [ 24%]
tests/test_auth.py::test_login_validate_input[test-a-Incorrect password.] PASSED                                                                                                                                                  [ 28%]
tests/test_auth.py::test_logout PASSED                                                                                                                                                                                            [ 32%]
tests/test_blog.py::test_index PASSED                                                                                                                                                                                             [ 36%]
tests/test_blog.py::test_login_required[/create] PASSED                                                                                                                                                                           [ 40%]
tests/test_blog.py::test_login_required[/1/update] PASSED                                                                                                                                                                         [ 44%]
tests/test_blog.py::test_login_required[/1/delete] PASSED                                                                                                                                                                         [ 48%]
tests/test_blog.py::test_author_required PASSED                                                                                                                                                                                   [ 52%]
tests/test_blog.py::test_exists_required[/2/update] PASSED                                                                                                                                                                        [ 56%]
tests/test_blog.py::test_exists_required[/2/delete] PASSED                                                                                                                                                                        [ 60%]
tests/test_blog.py::test_create PASSED                                                                                                                                                                                            [ 64%]
tests/test_blog.py::test_update PASSED                                                                                                                                                                                            [ 68%]
tests/test_blog.py::test_create_update_validate[/create] PASSED                                                                                                                                                                   [ 72%]
tests/test_blog.py::test_create_update_validate[/1/update] PASSED                                                                                                                                                                 [ 76%]
tests/test_blog.py::test_delete PASSED                                                                                                                                                                                            [ 80%]
tests/test_db.py::test_get_close_db PASSED                                                                                                                                                                                        [ 84%]
tests/test_db.py::test_init_db_command PASSED                                                                                                                                                                                     [ 88%]
tests/test_factory.py::test_config PASSED                                                                                                                                                                                         [ 92%]
tests/test_factory.py::test_hello PASSED                                                                                                                                                                                          [ 96%]
tests/test_flaskr.py::test_empty_db ERROR                                                                                                                                                                                         [100%]

================================================================================================================ ERRORS =================================================================================================================
____________________________________________________________________________________________________ ERROR at setup of test_empty_db ____________________________________________________________________________________________________

    <span class="pl-k">@</span>pytest.fixture
    <span class="pl-k">def</span> <span class="pl-en">client</span>():
&gt;       db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
E       NameError: name 'flaskr' is not defined

tests/test_flaskr.py:11: NameError
======================================================================================================== short test summary info ========================================================================================================
ERROR tests/test_flaskr.py::test_empty_db - NameError: name 'flaskr' is not defined
====================================================================================================== 24 passed, 1 error in 1.30s ======================================================================================================</pre></div>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Python version: Python 3.6.9</li>
<li>Flask version: 1.1.2</li>
<li>Werkzeug version: 1.0.1</li>
</ul>