[TIP] Generating fixture data with the fixture module

Rachel Sanders rachel at trustrachel.com
Thu Nov 22 10:01:13 PST 2012


Yep, that's the module I'm talking about.

I'm not quite sure what you're asking. The test classes load fixtures via
hand-coded SQLAlchemy objects.  Something like this:

def setup(self):

    objects = [
      Employee(name="engineer", role="engineer"),
      Employee(name="manager", role="manager"),
    ]

    for item in objects:
        db.session.add(item)

    db.session.commit()

It works okay for small sets of data, but relating objects/primary keys are
annoying and I can't refer to particular objects easily. The fixture module
looked like it handled that part of it nicely.

(Also, the developer before me didn't want to create separate data for each
test and instead just created a giant shared one. I'm trying to kill it -
it slows down the tests tremendously and makes them hard to debug.)

It looks like testscenario is for generating *tests*, not fixture data, or
am I reading it wrong? The only examples I could find were for that.

On Wed, Nov 21, 2012 at 6:25 PM, Martin Pool <mbp at sourcefrog.net> wrote:

> Are you talking about <http://pypi.python.org/pypi/fixtures> or something
> else?  What does your current fixture-using code look like?
>
> For things where you want to repeat a test over a possibly dynamically
> generated set of conditions, <http://pypi.python.org/pypi/testscenarios>
> is good.
>
>
> On 22 November 2012 04:54, Rachel Sanders <rachel at trustrachel.com> wrote:
>
>> I'm working with the fixture module for the first time, trying to get a
>> better set of testing data so I can make our functional tests more complete.
>>
>> I'm finding the fixture module a bit clunky, and I'm hoping it's just
>> because I'm going at this wrong. This is a Flask/SQLAlchemy app in Python
>> 2.7, and we're using nose as a test runner.
>>
>> So I have a set of employees. Employees have roles. There's a few pages
>> in our app with  complex permissions, and I'd like to make sure those are
>> tested fully.
>>
>> I created a DataSet that has each type of role (there's about 15 roles in
>> our app):
>>
>> class EmployeeData(DataSet):
>>
>>   class Meta:
>>     storable = Employee
>>
>>   class engineer:
>>     username = "engineer"
>>     role = ROLE_ENGINEER
>>
>>   class manager:
>>     username = "manager"
>>     role = ROLE_MANAGER
>>
>>   class admin:
>>     username = "admin"
>>     role = ROLE_ADMIN
>>
>> and what I'd like to do is write a functional test that checks only the
>> right people can access a page. (The actual permissions are way more
>> complicated, I just wanted a toy example to show you.)
>>
>> Something like this:
>>
>> def test_only_admin_can_see_this_page():
>>
>>   for employee in Employee.query.all():
>>     login(employee)
>>
>>     with self.app.test_request_context('/'):
>>       response = self.test_client.get(ADMIN_PAGE)
>>       if employee.role == ROLE_ADMIN
>>         eq_(200, response.status_code)
>>       else:
>>         eq_(401, response.status_code)
>>
>>     logout(employee)
>>
>> Is there a way to generate the fixture data so we don't have to remember
>> to add a line to the fixtures every time we add a role? We have the
>> canonical list of all roles as configuration elsewhere in the app, so I
>> have that.
>>
>> I'm not wedded to any of this or the fixture module, so I'm happy to hear
>> suggestions!
>>
>> I also asked this question over at StackOverflow, so if any of the code
>> doesn't come through right, you can see it there:
>>
>> http://stackoverflow.com/questions/13480955/generating-fixture-data-with-pythons-fixture-module
>>
>> thanks in advance,
>> Rachel
>>
>> _______________________________________________
>> testing-in-python mailing list
>> testing-in-python at lists.idyll.org
>> http://lists.idyll.org/listinfo/testing-in-python
>>
>>
>
>
> --
> Martin
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121122/99b4c900/attachment.html>


More information about the testing-in-python mailing list