本文整理汇总了Python中flask.ext.webtest.TestApp类的典型用法代码示例。如果您正苦于以下问题:Python TestApp类的具体用法?Python TestApp怎么用?Python TestApp使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TestApp类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IssNowTest
class IssNowTest(TestCase):
"""Test the ISS-now API"""
def setUp(self):
self.app = app
self.w = TestApp(self.app)
def test_load(self):
# Test the endpoints
r = self.w.get('/iss-now.json')
self.assertFalse(r.flashes)
r = self.w.get('/iss-now/')
self.assertFalse(r.flashes)
r = self.w.get('/iss-now/v1')
self.assertFalse(r.flashes)
def test_data(self):
r = self.w.get('/iss-now.json')
r.charset = 'utf8'
try:
data = json.loads(r.text)
except:
self.fail("ISS API not a valid JSON response")
# Success message
self.assertEqual(data['message'], "success", "ISS API Did not return 'sucess' message")
# timestamp exists
self.assertTrue('timestamp' in data)
# position data
self.assertTrue('iss_position' in data)
self.assertTrue('latitude' in data['iss_position'])
self.assertTrue('longitude' in data['iss_position'])
示例2: IssPassTest
class IssPassTest(TestCase):
"""Test ISS-pass API"""
def setUp(self):
self.app = app
self.w = TestApp(self.app)
self.endpoint = '/iss-pass.json'
def format_uri(self, lat, lon):
return self.endpoint + '?lat={0}&lon={1}'.format(lat, lon)
def test_berkeley(self):
berkeley = self.format_uri(37.8715926, -122.27274699999998)
r = self.w.get(berkeley)
r.charset = 'utf8'
data = json.loads(r.text)
self.assertEqual(r.status_code, 200)
def test_no_passes_found(self):
mcmurdo_station = self.format_uri(-77.8418779, 166.6863449)
r = self.w.get(mcmurdo_station)
self.assertEqual(r.status_code, 200)
data = r.json
self.assertEqual(len(data['response']), 0)
def test_bad_lat(self):
bad_lat = self.format_uri(-91, 50)
r = self.w.get(bad_lat, expect_errors=True)
self.assertEqual(r.status_code, 400)
示例3: AstrosTest
class AstrosTest(TestCase):
"""Test the number of astros API"""
def setUp(self):
self.app = app
self.w = TestApp(self.app)
def test_load_astros(self):
r = self.w.get('/astros.json')
self.assertFalse(r.flashes)
r = self.w.get('/astros/')
self.assertFalse(r.flashes)
r = self.w.get('/astros/v1')
self.assertFalse(r.flashes)
def test_data(self):
r = self.w.get('/astros.json')
r.charset = 'utf8'
try:
data = json.loads(r.text)
except:
self.fail("ISS API not a valid JSON responce")
# Success message
self.assertEqual(data['message'], "success", "ISS API Did not return 'sucess' message")
# data exists
self.assertTrue('number' in data)
self.assertEqual(type(data['number']), int)
self.assertTrue('people' in data)
示例4: setUp
def setUp(self):
self.app = create_app()
self.w_without_scoping = TestApp(self.app)
self.w = TestApp(self.app, db=db, use_session_scopes=True)
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
示例5: TestPosts
class TestPosts(DbTestCase, LoggedInUserCase):
def setUp(self):
self.w = TestApp(self.app)
self.user = UserFactory(password="myprecious")
self.user.save()
def test_can_create(self):
self._login(self.w, self.user)
res = self.w.get(url_for("user.add_post"))
form = res.forms["newPost"]
form['title'] = "test post please ignore"
form['subreddit'] = "reddit.com"
form['body'] = "This is a test post."
form['distinguish'] = False
form['sticky'] = False
form['scheduled_hour'] = 12
form['scheduled_minute'] = 00
res = form.submit().follow()
assert_equal(res.status_code, 200)
def test_can_submit_edit_form_without_changes(self):
self._login(self.w, self.user)
post = PostFactory()
self.user.posts.append(post)
self.user.save()
res = self.w.get(url_for('user.edit_post', id=post.id))
form = res.forms["newPost"]
res = form.submit().follow()
def test_can_edit(self):
self._login(self.w, self.user)
post = PostFactory()
self.user.posts.append(post)
self.user.save()
res = self.w.get(url_for('user.edit_post', id=post.id))
new_title = "this is a test title"
form = res.forms["newPost"]
previous_day_value = form['monday']
form['title'] = new_title
form['monday'] = not previous_day_value
res = form.submit().follow()
assert_equal(post.title, new_title)
assert_equal(post.days.monday, not previous_day_value)
示例6: TestLoggingIn
class TestLoggingIn(DbTestCase):
def setUp(self):
self.w = TestApp(self.app)
self.user = UserFactory(password="myprecious")
self.user.save()
def test_can_log_in(self):
# Goes to homepage
res = self.w.get("/")
# Fills out login form in navbar
form = res.forms['loginForm']
form['username'] = self.user.username
form['password'] = 'myprecious'
# Submits
res = form.submit().maybe_follow()
assert_equal(res.status_code, 200)
def _login(self, username, password):
res = self.w.get("/")
# Fills out login form in navbar
form = res.forms['loginForm']
form['username'] = username
form['password'] = password
# Submits
res = form.submit().maybe_follow()
return res
def test_sees_alert_on_log_out(self):
res = self._login(self.user.username, 'myprecious')
res = self.w.get(url_for('public.logout')).maybe_follow()
# sees alert
assert_in('You are logged out.', res)
def test_sees_error_message_if_password_is_incorrect(self):
# Goes to homepage
res = self.w.get("/")
# Fills out login form, password incorrect
form = res.forms['loginForm']
form['username'] = self.user.username
form['password'] = 'wrong'
# Submits
res = form.submit()
# sees error
assert_in("Invalid password", res)
def test_sees_error_message_if_username_doesnt_exist(self):
# Goes to homepage
res = self.w.get("/")
# Fills out login form, password incorrect
form = res.forms['loginForm']
form['username'] = 'unknown'
form['password'] = 'myprecious'
# Submits
res = form.submit()
# sees error
assert_in("Unknown user", res)
示例7: test_init
def test_init(self):
w = TestApp(self.app)
self.assertEqual(w.get('/').status_code, 200)
original_server_name = self.app.config['SERVER_NAME']
try:
self.app.config['SERVER_NAME'] = 'webtest-app.local'
w = TestApp(self.app)
self.assertEqual(w.get('/').status_code, 200)
finally:
self.app.config['SERVER_NAME'] = original_server_name
示例8: TestRegistering
class TestRegistering(DbTestCase):
def setUp(self):
self.w = TestApp(self.app)
def test_can_register(self):
# Goes to homepage
res = self.w.get("/")
# Clicks Create Account button
res = res.click("Create account")
# Fills out the form
form = res.forms["registerForm"]
form['username'] = 'foobar'
form['email'] = '[email protected]'
form['password'] = 'secret'
form['confirm'] = 'secret'
# Submits
res = form.submit().maybe_follow()
assert_equal(res.status_code, 200)
# A new user was created
assert_equal(len(User.query.all()), 1)
def test_sees_error_message_if_passwords_dont_match(self):
# Goes to registration page
res = self.w.get(url_for("public.register"))
# Fills out form, but passwords don't match
form = res.forms["registerForm"]
form['username'] = 'foobar'
form['email'] = '[email protected]'
form['password'] = 'secret'
form['confirm'] = 'secrets'
# Submits
res = form.submit()
# sees error message
assert_in("Passwords must match", res)
def test_sees_error_message_if_user_already_registered(self):
user = UserFactory(active=True) # A registered user
user.save()
# Goes to registration page
res = self.w.get(url_for("public.register"))
# Fills out form, but username is already registered
form = res.forms["registerForm"]
form['username'] = user.username
form['email'] = '[email protected]'
form['password'] = 'secret'
form['confirm'] = 'secret'
# Submits
res = form.submit()
# sees error
assert_in("Username already registered", res)
示例9: RouteTestCase
class RouteTestCase(BaseTestCase):
"""
Base test class for dealing with HTTP route handlers.
"""
def setUp(self):
super(RouteTestCase, self).setUp()
# Setup app for testing
self.app = TestApp(app, db=db, use_session_scopes=True)
self.app_context = app.app_context()
self.app_context.push()
# Setup database for testing
self.db = db
self.db.create_all()
# Create a new user
user = User(
email='[email protected]',
password='secret')
self.db.session.add(user)
self.db.session.commit()
# Assign the user to the session
with self.app.session_transaction() as sess:
sess['user_id'] = user.id
def tearDown(self):
# Clean up the test app and database
db.drop_all()
self.app_context.pop()
示例10: MyTests
class MyTests(TestCase):
def setUp(self):
self.app = app
self.w = TestApp(self.app)
def test_base_url_returns_response(self):
r = self.w.get('/')
self.assertFalse(r.flashes)
示例11: ViewsTestCase
class ViewsTestCase(TestCase):
def setUp(self):
self.app = app
self.w = TestApp(self.app)
def test_home(self):
r = self.w.get('/')
self.assertEquals(r.status_code, 200)
示例12: TestMainFeatures
class TestMainFeatures(unittest.TestCase):
def setUp(self):
self.app = app1
self.w = TestApp(self.app)
def test_single_template(self):
r = self.w.get('/')
self.assertFalse(r.flashes)
self.assertEqual(len(r.contexts), 1)
self.assertEqual(r.context['text'], 'Hello!')
self.assertEqual(r.template, 'template.html')
self.assertNotIn('qwerty', r.session)
def test_two_templates_and_flash_messages(self):
r = self.w.get('/').form.submit()
self.assertEqual(len(r.contexts), 2)
if flask_gte_0_10:
self.assertEqual(len(r.flashes), 2)
category, message = r.flashes[0]
self.assertEqual(message, 'You have pressed "Quit"...')
category, message = r.flashes[1]
self.assertEqual(message, 'Flash message that will never be shown')
else:
self.assertEqual(len(r.flashes), 1)
category, message = r.flashes[0]
self.assertEqual(message, 'You have pressed "Quit"...')
with self.assertRaises(AssertionError):
r.context # Because there are more than one used templates
self.assertEqual(
r.contexts['template.html']['text'],
'Goodbye!')
self.assertEqual(
r.contexts['extra-template.html']['extra_text'],
'Some text.')
示例13: FrontPageTest
class FrontPageTest(TestCase):
"""Test that the front page is rendering"""
def setUp(self):
self.app = app
self.w = TestApp(self.app)
def test(self):
r = self.w.get('/')
# Assert there was no messages flashed
self.assertFalse(r.flashes)
# Assert that we got an index template
self.assertEqual(r.template, 'index.html')
示例14: WebTestMixin
class WebTestMixin(object):
def create_app(self):
config = os.environ.get('KOZMIC_CONFIG', 'kozmic.config.TestingConfig')
return create_app(config)
def setup_app_and_ctx(self):
self.app = self.create_app()
self.ctx = self.app.app_context()
self.ctx.push()
self.w = TestApp(self.app)
def teardown_app_and_ctx(self):
self.ctx.pop()
def login(self, user_id):
with self.w.session_transaction() as sess:
sess['user_id'] = user_id
示例15: TestSQLAlchemyFeatures
class TestSQLAlchemyFeatures(unittest.TestCase):
def setUp(self):
self.app = create_app()
self.w_without_scoping = TestApp(self.app)
self.w = TestApp(self.app, db=db, use_session_scopes=True)
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
def tearDown(self):
db.drop_all()
self.app_context.pop()
def test_1(self):
user = User(name='Anton')
db.session.add(user)
db.session.commit()
r = self.w.get('/user/%i/' % user.id)
self.assertEqual(r.body, 'Hello, Anton!')
# Note: we did not commit the change to `user`!
user.name = 'Petr'
r = self.w_without_scoping.get('/user/%i/' % user.id)
self.assertEqual(r.body, 'Hello, Petr!')
r = self.w.get('/user/%i/' % user.id)
self.assertEqual(r.body, 'Hello, Anton!')
def test_2(self):
user = User(name='Anton')
db.session.add(user)
db.session.commit()
r = self.w.get('/user/%i/' % user.id)
self.assertEqual(r.body, 'Hello, Anton!')
r = self.w.post('/user/%i/preview/' % user.id, {
'greeting': 'Hi, %s.',
})
self.assertEqual(r.body, 'Hi, Anton.')
db.session.refresh(user)
r = self.w_without_scoping.post('/user/%i/preview/' % user.id, {
'greeting': 'Hi, %s.',
})
self.assertEqual(r.body, 'Hi, Anton.')
self.assertRaises(
sqlalchemy.exc.InvalidRequestError,
lambda: db.session.refresh(user))