How to use WebTest - 10 common examples

To help you get started, we’ve selected a few WebTest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github django-oscar / django-oscar / tests / functional / dashboard / test_product.py View on Github external
self.assertEqual(self.product.attr.float, 1.2)
        self.assertTrue(self.product.attr.boolean)
        self.assertEqual(self.product.attr.richtext, 'longread')
        self.assertEqual(self.product.attr.date, datetime.date(2016, 10, 12))
        self.assertEqual(self.product.attr.file.name, posixpath.join(self.image_folder, 'file1.txt'))
        self.assertEqual(self.product.attr.image.name, posixpath.join(self.image_folder, 'image1.png'))

        page = self.get(self.url)
        product_form = page.form
        product_form['attr_text'] = 'test2'
        product_form['attr_integer'] = '2'
        product_form['attr_float'] = '5.2'
        product_form['attr_boolean'] = ''
        product_form['attr_richtext'] = 'article'
        product_form['attr_date'] = '2016-10-10'
        product_form['attr_file'] = Upload('file2.txt', b"test", 'text/plain')
        product_form['attr_image'] = Upload('image2.png', self.generate_test_image('image2.png'), 'image/png')
        product_form.submit()

        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.attr.text, 'test2')
        self.assertEqual(self.product.attr.integer, 2)
        self.assertEqual(self.product.attr.float, 5.2)
        self.assertFalse(self.product.attr.boolean)
        self.assertEqual(self.product.attr.richtext, 'article')
        self.assertEqual(self.product.attr.date, datetime.date(2016, 10, 10))
        self.assertEqual(self.product.attr.file.name, posixpath.join(self.image_folder, 'file2.txt'))
        self.assertEqual(self.product.attr.image.name, posixpath.join(self.image_folder, 'image2.png'))
github naspeh / mailur / tests / test_web.py View on Github external
'name': 'A',
            'time': some,
            'title': 'A@t.com',
        },
        'b@t.com': {
            'addr': 'b@t.com',
            'hash': 'd2ed191b17f1b9abdfd509d877a765db',
            'name': 'b',
            'time': some,
            'title': 'b@t.com',
        }
    }

    web.post('/editor', {
        'draft_id': draft_id,
        'files': Upload('test.rst', b'txt', 'text/x-rst')
    }, status=200)
    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '3']
    assert [i['uid'] for i in msgs()] == ['3', '5']
    m = latest(parsed=1)
    assert local.data_msgids.get() == {
        '<101@mlr>': ['3'],
        m['meta']['msgid']: ['5']
    }
    assert m['flags'] == '\\Seen \\Draft test #personal'
    assert m['meta']['files'] == [
        {
            'filename': 'test.rst',
            'path': '2.1',
            'size': 3,
            'url': '/raw/3/2.1/test.rst',
        }
github django-oscar / django-oscar / tests / functional / dashboard / test_product.py View on Github external
def test_product_update_attribute_values(self):
        page = self.get(self.url)
        product_form = page.form
        # Send string field values due to an error
        # in the Webtest during multipart form encode.
        product_form['attr_text'] = 'test1'
        product_form['attr_integer'] = '1'
        product_form['attr_float'] = '1.2'
        product_form['attr_boolean'] = 'yes'
        product_form['attr_richtext'] = 'longread'
        product_form['attr_date'] = '2016-10-12'
        product_form['attr_file'] = Upload('file1.txt', b"test", 'text/plain')
        product_form['attr_image'] = Upload('image1.png', self.generate_test_image('image1.png'), 'image/png')
        product_form.submit()

        # Reloading model instance to re-initiate ProductAttributeContainer
        # with new attributes.
        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.attr.text, 'test1')
        self.assertEqual(self.product.attr.integer, 1)
        self.assertEqual(self.product.attr.float, 1.2)
        self.assertTrue(self.product.attr.boolean)
        self.assertEqual(self.product.attr.richtext, 'longread')
        self.assertEqual(self.product.attr.date, datetime.date(2016, 10, 12))
        self.assertEqual(self.product.attr.file.name, posixpath.join(self.image_folder, 'file1.txt'))
        self.assertEqual(self.product.attr.image.name, posixpath.join(self.image_folder, 'image1.png'))

        page = self.get(self.url)
        product_form = page.form
github RealGeeks / django-modelclone / tests / test_admin.py View on Github external
def test_clone_should_override_file_from_original_object_on_submit_if_new_file_was_chosen(self):
        response = self.app.get(self.multimedia_url, user='admin')
        response.form['image'] = Upload('tests/files/img-2.jpg')
        response.form['document'] = Upload('tests/files/file-2.txt')
        response.form.submit()

        multimedia = Multimedia.objects.latest('id')

        assert 'images/img-2.jpg' == str(multimedia.image)
        assert 'documents/file-2.txt' == str(multimedia.document)
github RealGeeks / django-modelclone / tests / test_admin.py View on Github external
def test_clone_should_override_file_from_original_object_on_submit_if_new_file_was_chosen(self):
        response = self.app.get(self.multimedia_url, user='admin')
        response.form['image'] = Upload('tests/files/img-2.jpg')
        response.form['document'] = Upload('tests/files/file-2.txt')
        response.form.submit()

        multimedia = Multimedia.objects.latest('id')

        assert 'images/img-2.jpg' == str(multimedia.image)
        assert 'documents/file-2.txt' == str(multimedia.document)
github django-oscar / django-oscar / tests / functional / dashboard / test_product.py View on Github external
def test_product_images_upload(self):
        page = self.get(self.url)
        product_form = page.form
        product_form['images-0-original'] = Upload('image1.png', self.generate_test_image('image1.png'), 'image/png')
        product_form['images-1-original'] = Upload('image2.png', self.generate_test_image('image2.png'), 'image/png')
        product_form.submit(name='action', value='continue').follow()
        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.images.count(), 2)
        page = self.get(self.url)
        product_form = page.form
        product_form['images-2-original'] = Upload('image3.png', self.generate_test_image('image3.png'), 'image/png')
        product_form.submit()
        self.product = Product.objects.get(pk=self.product.id)
        self.assertEqual(self.product.images.count(), 3)
        images = self.product.images.all()
        self.assertEqual(images[0].original.name, os.path.join(self.image_folder, 'image1.png'))
        self.assertEqual(images[0].display_order, 0)
        self.assertEqual(images[1].original.name, os.path.join(self.image_folder, 'image2.png'))
        self.assertEqual(images[1].display_order, 1)
        self.assertEqual(images[2].original.name, os.path.join(self.image_folder, 'image3.png'))
        self.assertEqual(images[2].display_order, 2)
github Pylons / webtest / webtest / __init__.py View on Github external
"""
        Executes the given request (``req``), with the expected
        ``status``.  Generally ``.get()`` and ``.post()`` are used
        instead.
        """
        __tracebackhide__ = True
        errors = StringIO()
        req.environ['wsgi.errors'] = errors
        if self.cookies:
            c = BaseCookie()
            for name, value in self.cookies.items():
                c[name] = value
            req.environ['HTTP_COOKIE'] = str(c).split(': ', 1)[1]
        req.environ['paste.testing'] = True
        req.environ['paste.testing_variables'] = {}
        app = lint.middleware(self.app)
        old_stdout = sys.stdout
        out = CaptureStdout(old_stdout)
        try:
            sys.stdout = out
            start_time = time.time()
            ## FIXME: should it be an option to not catch exc_info?
            res = req.get_response(app, catch_exc_info=True)
            end_time = time.time()
        finally:
            sys.stdout = old_stdout
            sys.stderr.write(out.getvalue())
        res.app = app
        res.test_app = self
        # We do this to make sure the app_iter is exausted:
        res.body
        res.errors = errors.getvalue()
github Pylons / webtest / tests / test_app.py View on Github external
def setUp(self):
        self.s = http.StopableWSGIServer.create(debug_app)
        self.s.wait()
github galaxyproject / pulsar / test / test_utils.py View on Github external
def server_for_test_app(app):
    try:
        from paste.exceptions.errormiddleware import ErrorMiddleware
        error_app = ErrorMiddleware(app.app, debug=True, error_log="errors.log")
        server = StopableWSGIServer.create(error_app)
    except ImportError:
        # paste.exceptions not available for Python 3.
        error_app = app.app
        server = StopableWSGIServer.create(error_app)
    try:
        server.wait()
        yield server
    finally:
        server.shutdown()
    # There seem to be persistent transient problems with the testing, sleeping
    # between creation of test app instances for greater than .5 seconds seems
    # to help (async loop length in code is .5 so this maybe makes some sense?)
    if "TEST_WEBAPP_POST_SHUTDOWN_SLEEP" in environ:
        time.sleep(int(environ.get("TEST_WEBAPP_POST_SHUTDOWN_SLEEP")))
github Pylons / webtest / webtest / sel.py View on Github external
def __init__(self, app=None, url=None, timeout=30000,
                 extra_environ=None, relative_to=None, **kwargs):
        self.app = self.server = None
        if app:
            super(SeleniumApp, self).__init__(app, relative_to=relative_to)
            self.server = http.StopableWSGIServer.create(app)
            self.server.wait()
            url = self.server.application_url
        assert is_available()
        self.session_id = None
        self._browser = Selenium()
        self._browser.start(url)
        self.extra_environ = extra_environ or {}
        self.timeout = timeout
        self.test_app = self