librelist archives

« back to archive

Unit testing Form objects

Unit testing Form objects

From:
Edd Grant
Date:
2014-08-07 @ 13:58
Hi all,

I'm new to Flask (and in fact to Python). I'm trying to follow TDD
practices whilst developing a simple Flask App.

I have a simple form as follows:

from flask_wtf import Form
from wtforms.validators import DataRequired


class AccessRequestForm(Form):
    jira_username = StringField('JIRA Username', [DataRequired()])
    jira_password = PasswordField('JIRA Password', [DataRequired()])

and I want to write unit tests to check that I've configured the form
correctly. My unit tests currently look like this:

from ..DeveloperAccessRequestForm import DeveloperAccessRequestForm#
import unittest


class DeveloperAccessRequestFormTest(unittest.TestCase):

    def should_fail_validation_when_no_data_provided_test(self):
        form = DeveloperAccessRequestFormBuilder()
        self.assertFalse(form.validate())

    def
should_pass_validation_when_all_inputs_are_entered_correctly_test(self):
        form =
DeveloperAccessRequestFormBuilder().with_valid_jira_username()\

.with_valid_jira_password()\

.create()
        self.assertTrue(form.validate())


class DeveloperAccessRequestFormBuilder:

    def __init__(self):
        self.data = {}

    def create(self):
        return DeveloperAccessRequestForm(self.data)

    def with_valid_jira_username(self):
        self.data['jira_username'] = 'edd.grant'
        return self

    def with_valid_jira_password(self):
        self.data['jira_password'] = 'This is clearly not my real JIRA
password'
        return self

The problem I'm having is that when I get to the line which tries to
instantiate my form I get the following error from Flask:

  File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 34,
in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

Grateful for your expertise on how I overcome this.

Many thanks,

Edd

-- 
Web: http://www.eddgrant.com
Email: edd@eddgrant.com
Mobile: +44 (0) 7861 394 543

Re: [flask] Unit testing Form objects

From:
Сергей Антипов
Date:
2014-08-07 @ 14:51
Hi!
Read about test_request_context
<http://flask.pocoo.org/docs/api/#flask.Flask.test_request_context>
http://flask.pocoo.org/docs/quickstart/#context-locals


2014-08-07 17:58 GMT+04:00 Edd Grant <edd@eddgrant.com>:

> Hi all,
>
> I'm new to Flask (and in fact to Python). I'm trying to follow TDD
> practices whilst developing a simple Flask App.
>
> I have a simple form as follows:
>
> from flask_wtf import Form
> from wtforms.validators import DataRequired
>
>
> class AccessRequestForm(Form):
>     jira_username = StringField('JIRA Username', [DataRequired()])
>     jira_password = PasswordField('JIRA Password', [DataRequired()])
>
> and I want to write unit tests to check that I've configured the form
> correctly. My unit tests currently look like this:
>
> from ..DeveloperAccessRequestForm import DeveloperAccessRequestForm#
> import unittest
>
>
> class DeveloperAccessRequestFormTest(unittest.TestCase):
>
>     def should_fail_validation_when_no_data_provided_test(self):
>         form = DeveloperAccessRequestFormBuilder()
>         self.assertFalse(form.validate())
>
>     def
> should_pass_validation_when_all_inputs_are_entered_correctly_test(self):
>         form =
> DeveloperAccessRequestFormBuilder().with_valid_jira_username()\
>
> .with_valid_jira_password()\
>
> .create()
>         self.assertTrue(form.validate())
>
>
> class DeveloperAccessRequestFormBuilder:
>
>     def __init__(self):
>         self.data = {}
>
>     def create(self):
>         return DeveloperAccessRequestForm(self.data)
>
>     def with_valid_jira_username(self):
>         self.data['jira_username'] = 'edd.grant'
>         return self
>
>     def with_valid_jira_password(self):
>         self.data['jira_password'] = 'This is clearly not my real JIRA
> password'
>         return self
>
> The problem I'm having is that when I get to the line which tries to
> instantiate my form I get the following error from Flask:
>
>   File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 34,
> in _find_app
>     raise RuntimeError('working outside of application context')
> RuntimeError: working outside of application context
>
> Grateful for your expertise on how I overcome this.
>
> Many thanks,
>
> Edd
>
> --
> Web: http://www.eddgrant.com
> Email: edd@eddgrant.com
> Mobile: +44 (0) 7861 394 543
>
>

Re: [flask] Unit testing Form objects

From:
Edd Grant
Date:
2014-08-07 @ 15:20
Thanks, will have a read of those.

Edd


On 7 August 2014 15:51, Сергей Антипов <antipov.serge@gmail.com> wrote:

> Hi!
> Read about test_request_context
> <http://flask.pocoo.org/docs/api/#flask.Flask.test_request_context>
> http://flask.pocoo.org/docs/quickstart/#context-locals
>
>
> 2014-08-07 17:58 GMT+04:00 Edd Grant <edd@eddgrant.com>:
>
> Hi all,
>>
>> I'm new to Flask (and in fact to Python). I'm trying to follow TDD
>> practices whilst developing a simple Flask App.
>>
>> I have a simple form as follows:
>>
>> from flask_wtf import Form
>> from wtforms.validators import DataRequired
>>
>>
>> class AccessRequestForm(Form):
>>     jira_username = StringField('JIRA Username', [DataRequired()])
>>     jira_password = PasswordField('JIRA Password', [DataRequired()])
>>
>> and I want to write unit tests to check that I've configured the form
>> correctly. My unit tests currently look like this:
>>
>> from ..DeveloperAccessRequestForm import DeveloperAccessRequestForm#
>> import unittest
>>
>>
>> class DeveloperAccessRequestFormTest(unittest.TestCase):
>>
>>     def should_fail_validation_when_no_data_provided_test(self):
>>         form = DeveloperAccessRequestFormBuilder()
>>         self.assertFalse(form.validate())
>>
>>     def
>> should_pass_validation_when_all_inputs_are_entered_correctly_test(self):
>>         form =
>> DeveloperAccessRequestFormBuilder().with_valid_jira_username()\
>>
>> .with_valid_jira_password()\
>>
>> .create()
>>         self.assertTrue(form.validate())
>>
>>
>> class DeveloperAccessRequestFormBuilder:
>>
>>     def __init__(self):
>>         self.data = {}
>>
>>     def create(self):
>>         return DeveloperAccessRequestForm(self.data)
>>
>>     def with_valid_jira_username(self):
>>         self.data['jira_username'] = 'edd.grant'
>>         return self
>>
>>     def with_valid_jira_password(self):
>>         self.data['jira_password'] = 'This is clearly not my real JIRA
>> password'
>>         return self
>>
>> The problem I'm having is that when I get to the line which tries to
>> instantiate my form I get the following error from Flask:
>>
>>   File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line
>> 34, in _find_app
>>     raise RuntimeError('working outside of application context')
>> RuntimeError: working outside of application context
>>
>> Grateful for your expertise on how I overcome this.
>>
>> Many thanks,
>>
>> Edd
>>
>> --
>> Web: http://www.eddgrant.com
>> Email: edd@eddgrant.com
>> Mobile: +44 (0) 7861 394 543
>>
>>
>


-- 
Web: http://www.eddgrant.com
Email: edd@eddgrant.com
Mobile: +44 (0) 7861 394 543

Re: [flask] Unit testing Form objects

From:
Edd Grant
Date:
2014-08-08 @ 07:50
Got it working after reading the first link you mentioned and understanding
the necessity of the test context. Many thanks for replying so quickly!

Edd
On 7 Aug 2014 15:55, "Сергей Антипов" <antipov.serge@gmail.com> wrote:

> Hi!
> Read about test_request_context
> <http://flask.pocoo.org/docs/api/#flask.Flask.test_request_context>
> http://flask.pocoo.org/docs/quickstart/#context-locals
>
>
> 2014-08-07 17:58 GMT+04:00 Edd Grant <edd@eddgrant.com>:
>
>> Hi all,
>>
>> I'm new to Flask (and in fact to Python). I'm trying to follow TDD
>> practices whilst developing a simple Flask App.
>>
>> I have a simple form as follows:
>>
>> from flask_wtf import Form
>> from wtforms.validators import DataRequired
>>
>>
>> class AccessRequestForm(Form):
>>     jira_username = StringField('JIRA Username', [DataRequired()])
>>     jira_password = PasswordField('JIRA Password', [DataRequired()])
>>
>> and I want to write unit tests to check that I've configured the form
>> correctly. My unit tests currently look like this:
>>
>> from ..DeveloperAccessRequestForm import DeveloperAccessRequestForm#
>> import unittest
>>
>>
>> class DeveloperAccessRequestFormTest(unittest.TestCase):
>>
>>     def should_fail_validation_when_no_data_provided_test(self):
>>         form = DeveloperAccessRequestFormBuilder()
>>         self.assertFalse(form.validate())
>>
>>     def
>> should_pass_validation_when_all_inputs_are_entered_correctly_test(self):
>>         form =
>> DeveloperAccessRequestFormBuilder().with_valid_jira_username()\
>>
>> .with_valid_jira_password()\
>>
>> .create()
>>         self.assertTrue(form.validate())
>>
>>
>> class DeveloperAccessRequestFormBuilder:
>>
>>     def __init__(self):
>>         self.data = {}
>>
>>     def create(self):
>>         return DeveloperAccessRequestForm(self.data)
>>
>>     def with_valid_jira_username(self):
>>         self.data['jira_username'] = 'edd.grant'
>>         return self
>>
>>     def with_valid_jira_password(self):
>>         self.data['jira_password'] = 'This is clearly not my real JIRA
>> password'
>>         return self
>>
>> The problem I'm having is that when I get to the line which tries to
>> instantiate my form I get the following error from Flask:
>>
>>   File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line
>> 34, in _find_app
>>     raise RuntimeError('working outside of application context')
>> RuntimeError: working outside of application context
>>
>> Grateful for your expertise on how I overcome this.
>>
>> Many thanks,
>>
>> Edd
>>
>> --
>> Web: http://www.eddgrant.com
>> Email: edd@eddgrant.com
>> Mobile: +44 (0) 7861 394 543
>>
>>
>