jarus / flask-testing Goto Github PK
View Code? Open in Web Editor NEWUnittest extensions for Flask
Home Page: http://pythonhosted.org/Flask-Testing/
License: Other
Unittest extensions for Flask
Home Page: http://pythonhosted.org/Flask-Testing/
License: Other
I attempted to follow a workflow with logging in as described in the Flask testing documentation . It seems to not be persisting the user login. I am not sure if this has to do with the scoped sessions or perhaps some trick that I am not aware of.
My '/login' endpoint works normally when running the server. API calls are being executed with the @login_required
decorator during functional testing, but using this framework I am unable to test my @login_required calls. Let me know if I am missing something. Thanks
Hi
Great plugin. Do you have any schedule for cutting a new release? I'm missing #45 in particular.
It's my first time doing Python testing.
In the docs, I saw the following at the end of a testing file:
if __name__ == '__main__':
unittest.main()
However, in the example given, it does not have anything like this, so I decided to not put the above code. Do I need this code to run this test? Or does it work without?
Thanks for the help.
I add this to my base TestCase:
import re
from functools import partial
def __getattr__(self, name):
name = re.sub(r'_([a-z])', lambda m: m.group(1).upper(), name)
return partial(getattr(TestCase, name), self)
# ^ The parent class
Lets me do self.assert_equals
etc. Adding it should also mean you can drop all the internal snake case aliases. Doesn't give you API documentation though you could add a note about this behaviour in the docs with some examples. Alternatively loop dir(TestCase) and dynamically alias any camel cased attributes, should take care of API docs.
I've tested that it avoids recursive stack exhaustion and that it properly raises an AttributeError when the camel case version doesn't exist. This is why we're doing getattr on the parent class and using partial to inject self.
It looks like Flask-Testing use assert which is bad
#!python
======================================================================
FAIL: test_login_post (tests.tests.LoginTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/amirouche/projects/flask-door/tests/tests.py", line 50, in test_login_post
self.assertRedirects(response, '/')
File "/home/amirouche/projects/flask-door/Flask_Testing-0.3-py2.7.egg/flaskext/testing.py", line 164, in assertRedirects
assert response.location == "http://localhost" + location
AssertionError
Twill itself has not been updated in 6 years and is not compatible with python 3. This means that flask-testing will never be able to support python 3 as long as it depends on twill.
Because of the pushed app context before each test case, multiple client requests within a test will see each other's (un-committed) sessions. This goes against the docs:
Another gotcha is that Flask-SQLAlchemy also removes the session instance at the end of every request (as should any threadsafe application using SQLAlchemy with scoped_session). Therefore the session is cleared along with any objects added to it every time you call client.get() or another client method.
Here's a test case that fails:
python
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.testing import TestCase
app = Flask(name)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
@app.route('/')
def index():
u = db.session.query(User).first()
u.name = 'bob'
return ''
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
class SessionTest(TestCase):
def create_app(self):
return app
def test_remove(self):
db.drop_all()
db.create_all()
u = User()
u.name = 'joe'
db.session.add(u)
db.session.commit()
client = app.test_client()
client.get('/')
assert u.name == 'joe' # fails!
assert u not in db.session # would fail if it reached here
The documentation does not specify how the tests are to be run. Perhaps this should be added.
This isn't a bug in LiveServerTestCase itself but with running the flask application using wekzeug with multiprocessing when the use_reloader option set to true. It can be reproduced with the following snippet
import unittest
import multiprocessing
import flask
import time
import random
class FlaskAppMultiProcTest(unittest.TestCase):
def runTest(self):
app = flask.Flask(__name__)
proc = multiprocessing.Process(target=app.run
, kwargs = {"use_reloader":True})
proc.start()
time.sleep(1)
proc.terminate()
if __name__ == "__main__":
unittest.main()
use_reloader
to False
results in single execution.unittest.main()
with FlaskAppMultiProcTest().runTest()
will also result in double execution.Can we use mongodb instead of SQL? Does the framework support mongodb?
I have a test case in which I do a couple requests using the test client; after every request I need to check the context and I use the get_context_variable
method. The problem I'm experiencing is that the value returned by this function is always the expected value from the first requests, subsequent requests don't affect the value returned by this method.
.................................................FF...
======================================================================
FAIL: test_assert_not_process_the_template (tests.test_utils.TestRenderTemplates)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/mnt/gen2/TmpDir/portage/dev-python/flask-testing-0.4.2/work/Flask-Testing-0.4.2/tests/test_utils.py", line 192, in test_assert_not_process_the_template
assert "" != response.data
AssertionError
======================================================================
FAIL: test_assert_the_real_render_template_is_restored (tests.test_utils.TestRestoreTheRealRender)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/mnt/gen2/TmpDir/portage/dev-python/flask-testing-0.4.2/work/Flask-Testing-0.4.2/tests/test_utils.py", line 209, in test_assert_the_real_render_template_is_restored
assert "" != response.data
AssertionError
----------------------------------------------------------------------
Ran 54 tests in 5.295s
FAILED (failures=2)
Occurs under python2.7 and pypy. CAn you replicate? Do you require anything further?
This is probably related to the change in #24.
Using python 2.7.5 and Flask-Testing==0.4.1
tests.py
#!/usr/bin/env python2.7
import unittest
if __name__ == "__main__":
suite = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(suite)
./tests/testApi.py
from flask.ext.testing import TestCase
class TestApi(TestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
return app
def test_server_is_up_and_running(self):
pass
test_helloworld (testHelloWorld.TestHelloWorld) ... ok
Traceback (most recent call last):
File "./tests.py", line 6, in <module>
unittest.TextTestRunner(verbosity=2).run(suite)
File "/usr/lib/python2.7/unittest/runner.py", line 151, in run
test(result)
File "/usr/lib/python2.7/unittest/suite.py", line 70, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.7/unittest/suite.py", line 108, in run
test(result)
File "/usr/lib/python2.7/unittest/suite.py", line 70, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.7/unittest/suite.py", line 108, in run
test(result)
File "/usr/lib/python2.7/unittest/suite.py", line 70, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.7/unittest/suite.py", line 108, in run
test(result)
File "/home/bhafichuk/.virtualenvs/kyc/local/lib/python2.7/site-packages/flask_testing/utils.py", line 90, in __call__
self._post_teardown()
File "/home/bhafichuk/.virtualenvs/kyc/local/lib/python2.7/site-packages/flask_testing/utils.py", line 116, in _post_teardown
if self._ctx is not None:
AttributeError: 'TestApi' object has no attribute '_ctx'
One of my tests look like this:
def test_get_network_info(self):
with patch('subprocess.check_output', Mock(return_value=values)):
And gives the following error:
======================================================================
ERROR: test_get_network_info (tests.test_tools.ToolsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tim/Documents/overseer/app/tests/test_tools.py", line 21, in test_get_network_info
with patch('subprocess.check_output', Mock(return_value=mock_network_info.read())):
File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1268, in __enter__
original, local = self.get_original()
File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1242, in get_original
"%s does not have the attribute %r" % (target, name)
AttributeError: <module 'subprocess' from '/usr/local/lib/python2.7/dist-packages/twill/other_packages/subprocess.pyc'> does not have the attribute 'check_output'
My understanding of the problem is that mock
is trying to mock twill's subprocess
module instead of the python one.
Am I doing something wrong ? Should I try to patch the subprocess
module differently ?
Here is the twill's version of subprocess wich looks like it has been copy pasted from an old version of python ?
https://github.com/ctb/twill/blob/master/twill/other_packages/subprocess.py
assertContext
, assertRedirects
in class TestCase
does not have a message parameter for customizing error message.
Since every other assert functions have them I see no reason not to implement it.
I had to replace the terminate method with the following to get coverage data from the child server process to be written to the file. *nix specific, but it works
def _terminate_live_server(self):
os.kill(self._process.pid, signal.SIGINT)
self._process.join()
Without this, none of the code run by the server during selenium tests would be counted in coverage reports.
If no messages are flashed to compare against, then assertMessageFlashed
will always fail silently without raising AssertionError
.
when i post username and password to def login();
it will raise a error
AssertionError: Popped wrong request context. (<RequestContext 'http://localhost/login' [POST] of adminapp> instead of <RequestContext 'http://localhost/' [GET] of adminapp>)
then i comment this line #login_user(Role.query.get(username=username))
test passing....
I have a flask app which uses multiprocessing
when computing stuff. When I add import flask.ext.testing
at the top of my test module (and nothing else, not using it yet), I get this when I run my tests:
Traceback (most recent call last):
File "mcbench_tests.py", line 5, in <module>
import app
File "/Users/isbadawi/code/py/mcbench/app.py", line 5, in <module>
import mcbench.client
File "/Users/isbadawi/code/py/mcbench/mcbench/client.py", line 4, in <module>
from mcbench.benchmark import Benchmark, BenchmarkSet
File "/Users/isbadawi/code/py/mcbench/mcbench/benchmark.py", line 5, in <module>
import multiprocessing
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/__init__.py", line 65, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/util.py", line 40, in <module>
from subprocess import _args_from_interpreter_flags
ImportError: cannot import name _args_from_interpreter_flags
(backend)www@backend:/var/www/msgapp/backend/project/tests$ cat test_app.py
import urllib2
from flask.ext.testing import LiveServerTestCase
class MyTest(LiveServerTestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
# Default port is 5000
app.config['LIVESERVER_PORT'] = 8943
return app
def test_server_is_up_and_running(self):
response = urllib2.urlopen(self.get_server_url())
self.assertEqual(response.code, 200)
(backend)www@backend:/var/www/msgapp/backend/project/tests$ nosetests
E
======================================================================
ERROR: test_server_is_up_and_running (test_app.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/var/www/msgapp/backend/local/lib/python2.7/site-packages/nose/case.py", line 133, in run
self.runTest(result)
File "/var/www/msgapp/backend/local/lib/python2.7/site-packages/nose/case.py", line 151, in runTest
test(result)
File "/var/www/msgapp/backend/local/lib/python2.7/site-packages/flask_testing/utils.py", line 328, in __call__
self._post_teardown()
File "/var/www/msgapp/backend/local/lib/python2.7/site-packages/flask_testing/utils.py", line 358, in _post_teardown
self._process.terminate()
AttributeError: 'NoneType' object has no attribute 'terminate'
----------------------------------------------------------------------
Ran 0 tests in 0.056s
FAILED (errors=1)
On py2.7.3 and following the code from https://pythonhosted.org/Flask-Testing/#testing-with-sqlalchemy I have created a test file as follows:
#test.py
import unittest
from flask.ext.testing import TestCase
from flask_issue import create_app
from model import User, db
class SomeTest(TestCase):
SQLALCHEMY_DATABASE_URI = "sqlite:////home/mez/code/flask_issue/test.db"
TESTING = True
def create_app(self):
# pass in test configuration
return create_app(self)
def setUp(self):
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_something(self):
user = User()
db.session.add(user)
db.session.commit()
# this works
assert user in db.session
response = self.client.get("/")
print response
# this raises an AssertionError
assert user in db.session
if __name__ == '__main__':
unittest.main()
I also have the following application and model files:
# flask_issue.py
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
def create_app(config_obj=None):
new_app = Flask(__name__)
if config_obj is not None:
new_app.config.from_object(config_obj)
from model import db
db.init_app(new_app)
return new_app
app = create_app()
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
and
# model.py
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
In the given test.py setup code, the overriden create_app(self)
method calls the imported create_app(self)
from flask_issue. This piece of code assumptions about implementation details. This code only works if the user
new_app.config.from_object(config_obj)
instead of new_app.config.update(config_dict)
or new_app.config.from_pyfile
These assumptions should be explicitly mentioned.
Then, running these tests produces the following output:
mez@red:~/code/flask_issue$ python test.py
<TestResponse streamed [404 NOT FOUND]>
.
----------------------------------------------------------------------
Ran 1 test in 0.456s
OK
This output does not match the tutorial's comments. The comment about assert user in db.session
raising an AssertionError seems to be wrong.
Additionally, the given self.client.get("/")
is not functioning. The output is a 404 because the routes are not being registered. Adding print self.app.url_map
after the print response
confirms this: Map([<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])
The work-around is to manually register the routes when returning the newly created app:
class SomeTest(TestCase):
SQLALCHEMY_DATABASE_URI = "sqlite:////home/mez/code/flask_issue/test.db"
TESTING = True
def create_app(self):
# pass in test configuration
new_app = create_app(self)
from flask_issue import hello_world
new_app.add_url_rule('/', 'hello_world', hello_world)
return new_app
Yields the output:
mez@red:~/code/flask_issue$ python test.py
<TestResponse streamed [200 OK]>
Map([<Rule '/' (HEAD, OPTIONS, GET) -> hello_world>,
<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])
.
----------------------------------------------------------------------
Ran 1 test in 0.473s
OK
The resulting functioning code has some key differences to the given tutorial code. Additionally, adding the routes manually is not maintainable. Is this the intended behavior?
In _pre_setup
(see commti 93c6365), a reference is set on the test case to the application object:
self.app = self.create_app()
But this reference is not removed on _post_teardown
. That means that we're leaking applications in situations where test cases are kept track of (e.g. with nose
).
How do I test this line?
return render_template(u'http_error.html', error_detail=detail)
I am a beginner as far as flask testing is concerned.
There is an unanswered question similar to this on stackoverflow.
http://stackoverflow.com/questions/23987564/test-flask-render-template-context
The change to flask.g has made flask test cases get the same g variable in subsequent calls to the test_client. Here is a test case:
from flask import Flask, g
app = Flask(__name__)
from flask.ext.testing import TestCase
import flask.ext.testing
@app.route("/")
def hello():
if hasattr(g, 'foo'):
return "g.foo is set to:" + g.foo
else:
g.foo = "bar"
return "Hello World!"
class GTest(TestCase):
def create_app(self):
return app
def test_g(self):
tc = self.client
r = tc.get('/')
print r.data
r = tc.get('/')
print r.data
self.assertEqual(r.data, "Hello World!")
if __name__ == "__main__":
flask.ext.testing.unittest.main()
I'm trying to test with Flask-Testing a Flask-SQLAlchemy model. More precisely a static method of this model that uses first_or_404()
and I cannot find a way to make my test work.
Here a self contained example that highlight the issue:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask.ext.testing import TestCase
db = SQLAlchemy()
class ModelToTest(db.Model):
__tablename__ = 'model_to_test'
identifier = db.Column(db.String(80), unique=True, nullable=False, primary_key=True)
@staticmethod
def get_by_identifier(identifier):
return ModelToTest.query.filter_by(identifier=identifier).first_or_404()
class Config:
DEBUG = True
TESTING = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///'
SQLALCHEMY_TRACK_MODIFICATIONS = False
class TestGetByIdentifier(TestCase):
def create_app(self):
app = Flask('test')
app.config.from_object(Config())
db.init_app(app)
return app
def setUp(self):
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_get_by_identifier(self):
self.assert404(ModelToTest.get_by_identifier('identifier'))
I got the error:
(my_env) PS C:\Dev\Test\Python\test_flask> nosetests-3.4.exe
E
======================================================================
ERROR: test_get_by_identifier (test_flask.TestGetByIdentifier)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Dev\Test\Python\test_flask\test_flask.py", line 37, in test_get_by_identifier
self.assert404(ModelToTest.get_by_identifier('identifier'))
File "C:\Dev\Test\Python\test_flask\test_flask.py", line 13, in get_by_identifier
return ModelToTest.query.filter_by(identifier=identifier).first_or_404()
File "c:\\my_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 431, in first_or_404
abort(404)
File "c:\\my_env\lib\site-packages\werkzeug\exceptions.py", line 646, in __call__
raise self.mapping[code](*args, **kwargs)
werkzeug.exceptions.NotFound: 404: Not Found
----------------------------------------------------------------------
Ran 1 test in 0.913s
So the line self.assert404(ModelToTest.get_by_identifier('identifier'))
does generate an exception in the first_or_404()
call and this exception is a werkzeug.exceptions.NotFound
, it does not seems to be what's expected by self.assert404()
.
Requirements to run this test are:
It is worth noting that when I use the function in the application it behaves as expected.
Thanks in advance.
Hi,
Since 0.6.0 I can't get my tests succeed anymore. They all fail with the following error:
======================================================================
ERROR: test_live_monitor (test_burpui.BurpuiRoutesTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/burp-ui/py/lib/python2.7/site-packages/nose/case.py", line 133, in run
self.runTest(result)
File "/tmp/burp-ui/py/lib/python2.7/site-packages/nose/case.py", line 151, in runTest
test(result)
File "/tmp/burp-ui/py/lib/python2.7/site-packages/flask_testing/utils.py", line 112, in __call__
self._post_teardown()
File "/tmp/burp-ui/py/lib/python2.7/site-packages/flask_testing/utils.py", line 170, in _post_teardown
message_flashed.disconnect(self._add_flash_message)
File "/tmp/burp-ui/py/lib/python2.7/site-packages/flask/signals.py", line 32, in _fail
raise RuntimeError('signalling support is unavailable '
RuntimeError: signalling support is unavailable because the blinker library is not installed.
Can you please update the available version in PyPI? The last update was done in 2014 and there are some major fixes. I'm totally ok using a nasty-looking line in requirements.txt, but Flask-Testing==0.5.0 or 0.4.3 would be really nice :)
Thanks all!
In Testing with SQLAlchemy section of the document:
def setUp(self):
db.create_all()
will cause error RuntimeError: application not registered on db instance and no application bound to current context
with class LiveServerTestCase
which can be fixed by:
def setUp(self):
with self.app.app_context():
db.create_all()
If you set render_templates
to False
, then flask's templating render gets monkeypatched with an empty render that doesn't do anything. Unfortunately, it doesn't seem to restore the original render, which causes all tests afterwards that require rendering to fail due to an empty response.
I have spotted the following code inside of TestCase._post_teardown
:
if hasattr(self, '_true_render'):
templating._render = self._true_render
Presumably, this was intended to restore the render? If so, _true_render
never gets created.. instead it's called _original_template_render
.
-robodude666
I'm trying to use unittest2 to add some of the newer unittest functionality to code running on python 2.6. It looks like flask.ext.testing.TestCase
inherits directly from unittest.TestCase
though, so some newer methods like assetIsNotNone
are not present.
Is there some way I can make this work out of the box?
Latest version of pip makes pip install flask-testing
fail because the current 0.4 version (from 07/2012) on PyPi fail because twill
is required and twill
is not on PyPi. The latest version of Flask-Testing works fine without twill
(if you don't use those features) and this can be fixed simply by providing the latest version of Flask-Testing on PyPi.
I'm using nose, and apparently, some of it's functionality, like the ability to yield tests, are not supported when inheriting from unittest.TestCase.
How do you feel about exposing the actual functionality of flask-testing's TestCase class as a bare TestCaseMixin class (inheriting from object) which could be used with nose.
In the TestCase class, there's this method:
def __call__(self, result=None):
"""
Does the required setup, doing it here
means you don't have to call super.setUp
in subclasses.
"""
try:
self._pre_setup()
super(TestCase, self).__call__(result)
finally:
self._post_teardown()
The problem is that if create_app() throws an exception when called in _pre_setup(), then the self._orig_response_class field is never set/created. And because of the exception, the 'finally' block is executed, and on line 101, _post_teardown() is trying to reference that field, and that throws another exception (saying that the field doesn't exist). So:
Hello,
I am relatively new to Flask development, and I began writing tests cases simply using unittest, but I recently found this package and became interested. Meanwhile, there are no full examples of testing a page with a form and verifying the result on the server's side. The strange intermediary that twill serves perhaps is confusing, as it is a wrapper around mechanize, and twill development has halted, whilst mechanize development is still ongoing...
I would very much appreciate a more full fledged example, such as with flaskr. Or perhaps simply validating that test_manually
actually produced an expected outcome server side.
I'm trying to install your package directly from github, using
pip install https://github.com/jarus/flask-testing
.
However, when I do so, I get an error from pip:
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/Library/Python/2.7/site-packages/pip/req.py", line 1197, in prepare_files
do_download,
File "/Library/Python/2.7/site-packages/pip/req.py", line 1375, in unpack_url
self.session,
File "/Library/Python/2.7/site-packages/pip/download.py", line 582, in unpack_http_url
unpack_file(temp_location, location, content_type, link)
File "/Library/Python/2.7/site-packages/pip/util.py", line 627, in unpack_file
and is_svn_page(file_contents(filename))):
File "/Library/Python/2.7/site-packages/pip/util.py", line 210, in file_contents
return fp.read().decode('utf-8')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte
Storing debug log for failure in /Users/Peter/Library/Logs/pip.log
Does anyone know how I can fix this? Thanks!
Hi,
There is an error in the documentation of the assert_context and assertContext functions.
The rst parser didn't find the right param keyword and the rendering is bad.
Stéphane
Running the tests using: nosetests -e twill
, I get:
.......................................F..F...
======================================================================
FAIL: test_assert_not_process_the_template (tests.test_utils.TestNotRenderTemplates)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/flask-testing/tests/test_utils.py", line 174, in test_assert_not_process_the_template
assert "" == response.data
AssertionError
======================================================================
FAIL: test_assert_the_real_render_template_is_restored (tests.test_utils.TestRestoreTheRealRender)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/flask-testing/tests/test_utils.py", line 205, in test_assert_the_real_render_template_is_restored
assert test_result.wasSuccessful()
AssertionError
----------------------------------------------------------------------
Ran 46 tests in 4.396s
FAILED (failures=2)
I custome my TestCase's teardown method, and it case a error.
When I run this case, it could raise a error about Flask-testing's self._ctx.pop()
error (link) .
That make me can't find real problem except I note r85 .
When the teardown method raise a error, flask-testing execute the _post_teardown will case this error :
AssertionError: Popped wrong request context. (<RequestContext 'http://localhost/connect/signup' [POST] of run> instead of <RequestContext 'http://localhost/' [GET] of run>)
Specifically I'm testing for status 400 for my web API, but others might be useful as well.
Not an issue, more of a question.
My Flask apps tend to use route decorators (not all of them in blueprints), which means they use a global app object. My create_app function imports my view module. But if that create_app function is called more than once, as it is in Flask-Testing, the views import only works once and my routes aren't picked up.
What's the recommended way of combining route decorators with Flask-Testing?
Thanks.
The documentation does not specify how the tests are to ne run. Perhaps this should be added.
The test matrix in CI currently uses multiple versions of Flask but does not cover the cases where components (ie. blinker) are installed and then not installed. Because of this, we were unable to catch the issue reported in: #91 but it would have been immediately obvious with the proper facilities.
Investigate a solution such that the build matrix can have different configurations of flask installations
Not sure if this should be a comment, or a suggestion, but it's a bit lost on me, why a testing framework would not be displaying the testing status (I found them on travis-ci, they do github badges), or the code coverage; which doesn't seem to be generated on travis, or exist on coveralls. Would it be possible to request that this gets added?
It looks like a new, possibly buggy version of Twill was released on 2014-04-24, which doesn't seem to install cleanly with pip.
Until this is resolved, would it be possible to pin the twill requirement in this package to the previous version (0.9)?
My tests (without any actual use of Twill) appear to have all broken, and re-running pip install -r requirements.txt
(which contains Flask-Testing) doesn't fix it.
Twill alters sys.path
( http://twill.idyll.org/problems.html )
and put its other_packages directory in front of everything. When I import subprocess module, instead of stdlib it imports from twill. When I change the order of my imports it works fine. It shouldn't be that way.
https://github.com/jarus/flask-testing/blob/master/flask_testing/utils.py#L209
This suppose you run test with localhost. In case one use subdomains, this doesn't work because subdomains required a FQDN like myserver.local
.
When the test client is named self.app
, i.e. you have a situation like
def setUp(self):
self.app = application.test_client()
Then when assertRedirects
tries to access the Flask instance at self.app
it instead gets the FlaskClient
instance and crashes as below.
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Users/bistenes/Code/socos/jive/server/src/tests/test_pages.py", line 118, in test_logs_in_user
self.assertRedirects(rv, '/manage')
File "/Users/bistenes/Code/socos/jive/server/src/lib/flask_testing/utils.py", line 278, in assertRedirects
server_name = self.app.config.get('SERVER_NAME') or 'localhost'
AttributeError: 'FlaskClient' object has no attribute 'config'
I'm pretty surprised this wasn't a problem before 0.6.0 -- you would think that resolving self.app
elsewhere, e.g. for self.app.get(...)
would also be a problem, but it apparently wasn't. The obvious solution is to not name the test client self.app
, but rather, say, self.client
.
This is mostly a request for documentation -- somewhere it should advise against doing this. It's also, of course, a note to those running into this confusing error in the future. Actual code change is probably not called for here.
The requests
library has a callable json attribute.
To make TestResponse.json
"optionally callable", we use the following work-around:
class JsonResponseMixin(object):
"""
Mixin with testing helper methods
"""
@cached_property
def json(self):
"""Return response data as a Python object.
The data is deserialized using the JSON format.
"""
# Note that during testing, this response class may already
# provide a JSON property. We re-implement it here for
# consistency.
value = loads(self.data)
# Adapt value to make it callable. Alas, the 'requests'
# library changed its response class such that the JSON
# property is callable.
cls = type(value)
return type(cls.__name__, (cls, ), {
'__call__': lambda v: cls(v),
'__module__': cls.__module__,
'__reduce__': lambda v: (cls, (cls(v), )),
})(value)
That is, you can call it or use it directly – because it's a dict whose callable returns the dict.
I'm getting the error below when I use to flask.ext.testing.LiveServerTestCase class with python 3.3
Traceback (most recent call last):
File "C:\Users\Reuber\env_stormy_flask\lib\site-packages\flask_testing\utils.py", line 345, in __call__
self._spawn_live_server()
File "C:\Users\Reuber\env_stormy_flask\lib\site-packages\flask_testing\utils.py", line 366, in _spawn_live_server
self._process.start()
File "C:\Python33\Lib\multiprocessing\process.py", line 111, in start
self._popen = Popen(self)
File "C:\Python33\Lib\multiprocessing\forking.py", line 248, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Python33\Lib\multiprocessing\forking.py", line 166, in dump
ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <class '_thread.lock'>: attribute lookup _thread.lock failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python33\Lib\runpy.py", line 160, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python33\Lib\runpy.py", line 73, in _run_code
exec(code, run_globals)
File "C:\Python33\Lib\unittest\__main__.py", line 19, in <module>
main(module=None)
File "C:\Python33\Lib\unittest\main.py", line 125, in __init__
self.runTests()
File "C:\Python33\Lib\unittest\main.py", line 265, in runTests
self.result = testRunner.run(self.test)
File "C:\Python33\Lib\unittest\runner.py", line 168, in run
test(result)
File "C:\Python33\Lib\unittest\suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "C:\Python33\Lib\unittest\suite.py", line 105, in run
test(result)
File "C:\Python33\Lib\unittest\suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "C:\Python33\Lib\unittest\suite.py", line 105, in run
test(result)
File "C:\Python33\Lib\unittest\suite.py", line 67, in __call__
return self.run(*args, **kwds)
File "C:\Python33\Lib\unittest\suite.py", line 105, in run
test(result)
File "C:\Users\Reuber\env_stormy_flask\lib\site-packages\flask_testing\utils.py", line 348, in __call__
self._terminate_live_server()
File "C:\Users\Reuber\env_stormy_flask\lib\site-packages\flask_testing\utils.py", line 373, in _terminate_live_server
self._process.terminate()
File "C:\Python33\Lib\multiprocessing\process.py", line 119, in terminate
self._popen.terminate()
AttributeError: 'NoneType' object has no attribute 'terminate'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.