我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用app.app.test_client()。
def test_currentsession(self): tester = app.test_client(self) response = tester.get('/api/v1/session' , content_type='application/json') #print response.data #self.assertIn(b'REDACTED', response.data) # def test_login_correct(self): # tester = app.test_client(self) # credentials = { "credentials" : { # "user" : "admin", # "password" : "nbv12345", # "server" : "172.28.225.163" # }} # response = tester.post( # '/api/v1/session' , # data = json.dumps(credentials), # content_type='application/json' # ) # self.assertIn(b'success', response.data)
def setUp(self): """Setup method for spinning up a test instance of app""" app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False self.app = app.test_client() self.app.testing = True self.authorization = { 'Authorization': "Basic {user}".format( user=base64.b64encode(b"test:asdf").decode("ascii") ) } self.content_type = 'application/json' self.dummy_name = 'dummy' self.dummy_user = json.dumps( {'username': 'dummy', 'languages': ['testLang']} ) self.dummy_lang = json.dumps( {'name': 'dummy', 'users': ['No one']} )
def setUp(self): self.ctx = app.app_context() self.ctx.push() db.drop_all() # just in case db.create_all() self.client = app.test_client()
def setup_class(cls): """ When a Test is created override the ``database`` attribute for all the tables with a SqliteDatabase in memory. """ for table in TABLES: table._meta.database = cls.TEST_DB table.create_table(fail_silently=True) cls.app = app.test_client()
def setUp(self): app.config['TESTING'] = True self.app = app.test_client() self.app_context = app.app_context() self.app_context.push()
def setUp(self): self.app = app.test_client()
def setUp(self): self.db_fd, self.db_filename = tempfile.mkstemp() # FIXME: this isn't actually working. app.config['DATABASE_URI'] = 'sqlite:///' + self.db_filename self.app = app.test_client() db.create_all()
def setUp(self): '''Creates a test client, disables logging, connects to the database and creates state and city tables for temporary testing purposes. ''' self.app = app.test_client() logging.disable(logging.CRITICAL) BaseModel.database.connect() BaseModel.database.create_tables([User, State, City, Place, PlaceBook]) '''Create table items for ForeignKeyField requirements.''' self.app.post('/users', data=dict( first_name="test", last_name="test", email="test", password="test" )) self.app.post('/states', data=dict( name="test" )) self.app.post('/states/1/cities', data=dict( name="test", state=1 )) self.app.post('/places', data=dict( owner=1, city=1, name="test", description="test", latitude=0, longitude=0 ))
def setUp(self): '''Creates a test client, disables logging, connects to the database and creates a table State for temporary testing purposes. ''' self.app = app.test_client() logging.disable(logging.CRITICAL) BaseModel.database.connect() BaseModel.database.create_tables([State])
def setUp(self): '''Creates a test client, disables logging, connects to the database and creates state and city tables for temporary testing purposes. ''' self.app = app.test_client() logging.disable(logging.CRITICAL) BaseModel.database.connect() BaseModel.database.create_tables([User, City, Place, State, PlaceBook]) '''Create items in tables for ForeignKeyField requirements''' self.app.post('/states', data=dict(name="test")) self.app.post('/states/1/cities', data=dict( name="test", state=1 )) self.app.post('/users', data=dict( first_name="test", last_name="test", email="test", password="test" )) '''Create two places.''' for i in range(1, 3): self.create_place('/places', 'test_' + str(i)) '''Create a book on place 1, belonging to user 1.''' self.app.post('/places/1/books', data=dict( place=1, user=1, date_start="2000/1/1 00:00:00", description="test", number_nights=10, latitude=0, longitude=0 ))
def setUp(self): '''Creates a test client, disables logging, connects to the database and creates state and city tables for temporary testing purposes. ''' self.app = app.test_client() logging.disable(logging.CRITICAL) BaseModel.database.connect() BaseModel.database.create_tables([State, City]) '''Create a state for routing purposes.''' self.app.post('/states', data=dict(name="test")) '''Create two new cities.''' for i in range(1, 3): res = self.create_city("test_" + str(i))
def setUp(self): '''Creates a test client and propagates the exceptions to the test client. ''' self.app = app.test_client() self.app.testing = True
def setUp(self): '''Creates a test client, disables logging, connects to the database and creates a table User for temporary testing purposes. ''' self.app = app.test_client() logging.disable(logging.CRITICAL) BaseModel.database.connect() BaseModel.database.create_tables([User, Place, Review, ReviewPlace, ReviewUser, City, State]) '''Add two new users.''' for i in range(1, 3): self.app.post('/users', data=dict(first_name="user_" + str(i), last_name="user_" + str(i), email="user_" + str(i), password="user_" + str(i))) '''Add a state.''' self.app.post('/states', data=dict(name="state_1")) '''Add a city.''' self.app.post('/states/1/cities', data=dict(name="city_1", state=1)) '''Add a place.''' self.app.post('/places', data=dict(owner=1, city=1, name="place_1", description="place_1", latitude=0, longitude=0))
def setUp(self): '''Creates a test client, disables logging, connects to the database and creates a table User for temporary testing purposes. ''' self.app = app.test_client() logging.disable(logging.CRITICAL) BaseModel.database.connect() BaseModel.database.create_tables([User])
def setUp(self): app.config['TESTING'] = True app.config['DEBUG'] = True self.client = app.test_client() super(TestCaseWeb, self).setUp()
def test_api(self): tester=app.test_client(self) response = tester.get('/', content_type='application/json') self.assertEqual(response.status_code,200)
def setUp(self): self.app = app self.app.config.from_object('app.config') self.app.config["NO_PASSWORD"] = False self.app.config["DEBUG"] = True self.app.config["TESTING"] = True self.app = app.test_client() db.create_all()
def test_controller_with_empty_db(self, db): """ Imitate situation when there is no any user available in the db and sqlalchemy output becomes an empty list []. Message 'There are no users in the database' instead of users table is expected as default behavior in this case. """ db.session.query.return_value.all.return_value = [] with app.test_client() as test_client: responce = test_client.get('/users/all') data = responce.data self.assertIn('There are no users in the database.', data)
def test_unavailable_db(self): """ Check if "Can not access database" message appears if db is unavailable. """ #break db uri to call exception inside controller app.config['SQLALCHEMY_DATABASE_URI'] = '' with app.test_client() as test_client: responce = test_client.get('/users/all') data = responce.data self.assertIn("Can not access database.", data) self.assertNotIn('<table class="table">', data)
def setUp(self): # creates a test client self.app = app.test_client() # propagate the exceptions to the test client self.app.testing = True
def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db') self.app = app.test_client() db.create_all()
def test_index(self): """initial test. ensure flask was set up correctly""" tester = app.test_client(self) response = tester.get('/', content_type='html/text') self.assertEqual(response.status_code, 200)
def setUp(self): """Set up a blank temp database before each test""" basedir = os.path.abspath(os.path.dirname(__file__)) app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \ os.path.join(basedir, TEST_DB) self.app = app.test_client() db.create_all()
def setUp(self): # Necessary to disable SSLify app.debug = True self.test_app = app.test_client() self.session = app.requests_session
def test_health_endpoint(self): """Assert that the health endpoint works.""" response = app.test_client().get('/health') self.assertEquals(response.data, ';-)')
def test_root_endpoint(self): """Assert that the / endpoint correctly redirects to login.uber.com.""" response = app.test_client().get('/') self.assertIn('login.uber.com', response.data)
def test_submit_endpoint_failure(self): """Assert that the submit endpoint returns no code in the response.""" with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = test_auth_token with Betamax(app.requests_session).use_cassette('submit_failure'): response = client.get('/submit?code=not_a_code') self.assertIn('None', response.data)
def test_products_endpoint_returns_success(self): """Assert that the products endpoint returns success. When a valid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = test_auth_token with Betamax(app.requests_session).use_cassette('products_success'): response = client.get('/products') self.assertIn('products', response.data) self.assertEquals(response.status_code, 200)
def test_time_estimates_endpoint_returns_success(self): """Assert that the time estimates endpoint returns success. When a valid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = test_auth_token with Betamax(app.requests_session).use_cassette('time_estimates_success'): response = client.get('/time') self.assertIn('times', response.data) self.assertEquals(response.status_code, 200)
def test_time_estimates_endpoint_returns_failure(self): """Assert that the time estimates endpoint returns failure. When an invalid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = 'NOT_A_CODE' with Betamax(app.requests_session).use_cassette('time_estimates_failure'): response = client.get('/time') self.assertEquals(response.status_code, 401)
def test_price_estimates_endpoint_returns_success(self): """Assert that the price estimates endpoint returns success. When a valid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = test_auth_token with Betamax(app.requests_session).use_cassette('price_estimates_success'): response = client.get('/price') self.assertIn('prices', response.data) self.assertEquals(response.status_code, 200)
def test_price_estimates_endpoint_returns_failure(self): """Assert that the price estimates endpoint returns failure. When an invalid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = 'NOT_A_CODE' with Betamax(app.requests_session).use_cassette('price_estimates_failure'): response = client.get('/price') self.assertEquals(response.status_code, 401)
def test_history_endpoint_returns_success(self): """Assert that the history endpoint returns success. When a valid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = test_auth_token with Betamax(app.requests_session).use_cassette('history_success'): response = client.get('/history') self.assertIn('history', response.data) self.assertEquals(response.status_code, 200)
def test_me_endpoint_returns_success(self): """Assert that the me endpoint returns success. When a valid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = test_auth_token with Betamax(app.requests_session).use_cassette('me_success'): response = client.get('/me') self.assertIn('picture', response.data) self.assertEquals(response.status_code, 200)
def test_me_endpoint_returns_failure(self): """Assert that the me endpoint returns failure. When an invalid key is passed in. """ with app.test_client() as client: with client.session_transaction() as session: session['access_token'] = 'NOT_A_CODE' with Betamax(app.requests_session).use_cassette('me_failure'): response = client.get('/me') self.assertEquals(response.status_code, 401)
def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s \ ' % app.config['DATABASE'] app.testing = True self.app = app.test_client() with app.app_context(): db.create_all() populate_db._run()
def setUp(self): app.config.from_object(os.environ['APP_SETTINGS']) self.app = app.test_client() db.create_all()