def test_it(): app = make_app() client = test.TestClient(app) response = client.get("/") assert response.status_code == 200 assert response.json() == {"message": "Welcome to API Star!"}
def client(request): if request.param == "asgi": app = ASyncApp(routes=routes) elif request.param == "wsgi": app = App(routes=routes) else: app = CORSApp(routes=routes) return test.TestClient(app)
def test_on_error_loads_the_same_component_instances(): # Given that I have an app that uses an AComponent and the AEventHooks app = App( components=[AComponent()], event_hooks=[AEventHooks], routes=[Route('/uses-a', method='GET', handler=uses_a)], ) # When I call an endpoint that raises an error with pytest.raises(RuntimeError): test.TestClient(app).get('/uses-a') # Then all the instances of A are the same assert len(A_INSTANCES) >= 2 for i in range(1, len(A_INSTANCES)): assert A_INSTANCES[i] is A_INSTANCES[i - 1]
from apistar import test from app import APP CLIENT = test.TestClient(APP) def test_todo_item_get(): response = CLIENT.get('/todos/1') assert response.status_code == 200 assert response.json() == {'id': 1, 'task': 'Buy milk'} def test_todo_item_put(): response = CLIENT.put('/todos/100', {'task': 'Buy butter'}) assert response.status_code == 200 assert response.json() == {'id': 100, 'task': 'Buy butter'} def test_todo_item_delete(): response = CLIENT.delete('/todos/23') assert response.status_code == 204 def test_todo_collection_post(): response = CLIENT.post('/todos/', json={ 'id': 2, 'task': 'Buy apples' }, allow_redirects=False) assert response.status_code == 201
session.close() return created_kitten app = App(routes=[ routing.Route('/kittens/create/', 'GET', create_kitten), routing.Route('/kittens/', 'GET', list_kittens), ], settings={ "DATABASE": { "URL": environ.get('DB_URL', 'sqlite:///test.db'), "METADATA": Base.metadata } }) client = test.TestClient(app) runner = CommandLineRunner(app) @pytest.fixture def clear_db(scope="function"): yield db_backend = SQLAlchemy.build(app.settings) db_backend.drop_tables() def test_list_create(monkeypatch, clear_db): def mock_get_current_app(): return app monkeypatch.setattr(apistar.cli, 'get_current_app', mock_get_current_app)
def client(request): if request.param == 'asgi': app = ASyncApp(routes=routes) else: app = App(routes=routes) return test.TestClient(app)
def cli_app_no_auth(napp): """apistar test client""" return test.TestClient(main_app)
def cli_anonymous(napp): """apistar test client""" return test.TestClient(main_app)
import unittest from apistar import test from app import app CLIENT = test.TestClient(app) class CeryxTestCase(unittest.TestCase): def setUp(self): self.client = CLIENT def test_list_routes(self): """ Assert that listing routes will return a JSON list. """ response = self.client.get('/api/routes') self.assertEqual(response.status_code, 200) self.assertEqual(type(response.json()), list) def test_create_route(self): """ Assert that creating a route, will result in the appropriate route. """ route_data = { 'source': 'test.dev', 'target': 'localhost:11235', } expected_response = {
def client(redis_client): client = test.TestClient(app) yield client
def client(): return test.TestClient(app)
def client(): client = test.TestClient(app) yield client local.local_memory_sessions = {}