Giter Club home page Giter Club logo

Comments (9)

shuttle1987 avatar shuttle1987 commented on May 18, 2024 3

Currently I'm using Flask and I'd like to use Tavern for some tests because it seems great. Is it possible to use the flask test client now?

In my conftest.py I have something like this:

@pytest.fixture
def client(tmpdir):
    """Create a test client to send requests to"""
    flask_app.config['BASE_UPLOAD_DIRECTORY'] = os.path.join(str(tmpdir), 'test_uploads')

    from api.upload_config import configure_uploads
    configure_uploads(flask_app)
    with flask_app.test_client() as c:
        yield c

Is there some supported way of using this with Tavern?

from tavern.

michaelboulton avatar michaelboulton commented on May 18, 2024 2

Just to clarify about testing flask with the test client - do you basically want to run flask tests that would be something like

test_client = app.test_client()
response = test_client.get("/scan", headers={"authorization": "bearer abc123"})
assert response.status_code == 200

but written in YAML

request:
  url: www.example.com/scan
  method: GET
  headers:
    authorization: bearer abc123
response:
  status_code: 200

Like that?

from tavern.

WhileLoop avatar WhileLoop commented on May 18, 2024 2

@Birne94

I managed to hack together a live server method that also produces code coverage reports here. https://github.com/WhileLoop/flask-tavern-coverage

The only catch is that you have to run a non tavern test first for the fixture to start and the server can't be in debug mode. Let us know if you come up with anything better.

from tavern.

Birne94 avatar Birne94 commented on May 18, 2024

Yes. I want to be able to omit the protocol://host:port part of request.url and would like to pass it to the test client instead.

from tavern.

Birne94 avatar Birne94 commented on May 18, 2024

Some more thoughts about how the test schema might look:

test_name: test
test_request_class:
  class: tavern.plugins.flask.FlaskTestRequest
  extra_kwargs:
    flask_app: myapp.factory.app

stages:
  - name: test
    request:
      url: /users/1/
      method: GET
    response:
      status_code: 200

FlaskTestRequest takes a Flask app instance or an absolute import path as parameter and instantiates it. All requests are then performed using this class.

test_request_class.class would default to the current RestRequest class, which of course might need some changes to be compatible. MQTT support could be adapted in a similar way.

One might decide to allow for full urls (including protocol and host) to be performed through requests, but this might be an implementation detail.

from tavern.

michaelboulton avatar michaelboulton commented on May 18, 2024

I wrote up a (rough) spec of what the plugin system is likely to look like here: #41 . Not sure when this will get implemented though.

The relevant parts with this is that the 'session' is probably just a context manager that yields the flask test client, the 'request' block calls test_client.post etc., and then most of the RestResponse can probably be reused for the 'verifier'. a (possible) example:

# flask_tavern_plugin.py
import contextlib

@contextlib.contextmanager
def get_session(app_module):
    from tavern import import_ext_function
    app = import_ext_function(app_module)
    yield app

# etc...
---
test_name: test local flask server works

plugins:
  flask:
    app_module: mymodule.server:app

stages:
  - name: post something
    flask_request:
      url: www.example.com/scan
      method: GET
      headers:
        authorization: bearer abc123
    response:
      status_code: 200

from tavern.

michaelboulton avatar michaelboulton commented on May 18, 2024

I've run into that before, fixtures in pytest are only triggered when it starts a python test, not any 'special' kinds of tests (like the YamlItem that Tavern returns). In the pytest source code it only seems to use fixtures when pytest-style (ie, not unittest) python tests are run. This quick hack does make autouse fixtures work at least

diff --git a/tavern/testutils/pytesthook.py b/tavern/testutils/pytesthook.py
index eef1c5e..e5638ba 100644
--- a/tavern/testutils/pytesthook.py
+++ b/tavern/testutils/pytesthook.py
@@ -54,6 +54,7 @@ class YamlFile(pytest.File):
 
             yield YamlItem(test_spec["test_name"], self, test_spec, self.fspath)
 
+from _pytest import fixtures
 
 class YamlItem(pytest.Item):
 
@@ -73,6 +74,18 @@ class YamlItem(pytest.Item):
         self.path = path
         self.spec = spec
 
+        self.cls = None
+        self.obj = object()
+        self.funcargs = []
+        self._fixtureinfo = self.session._fixturemanager.getfixtureinfo(
+            self.parent, self.obj, self.cls,
+            funcargs=None)
+        self._request = fixtures.FixtureRequest(self)
+
+    def setup(self):
+        super(YamlItem, self).setup()
+        fixtures.fillfixtures(self)
+
     def runtest(self):
         verify_tests(self.spec)

from tavern.

michaelboulton avatar michaelboulton commented on May 18, 2024

@Birne94 If you're still interested in this I have done a quick implementation of a tavern flask plugin. You can use the feature/plugins branch of this repo along with https://github.com/taverntesting/tavern-flask, which has an example/ folder

from tavern.

Birne94 avatar Birne94 commented on May 18, 2024

@michaelboulton thanks for the heads up. I have been following this project's updates and changes for the past weeks, but haven't gotten around trying the new features out.

It looks promising so far, I will play around with the plugin a bit in the next days.

from tavern.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.