Generating Fuzzy attribute before each test

21 views
Skip to first unread message

Caio Ignatz

unread,
Nov 28, 2017, 9:05:37 AM11/28/17
to Factory Boy

I'm creating test_cases(pytest) to perform integration tests of my web-application using mainly MongomockUnittest.Mock and Factory-Boy.

In each test_case I create a mock object to assert the behavior of my application's entities.

Through Test.App and Paste.deploy I've got an environment for tests, on which before each test's execution the environment is reseted:


def setUp(self):
        '''
        This method is called before each test's execution and it set's up the environment for tests execution. It
         creates, among other things:
        - Mongomock connect through mongoengine in the test execution context;
        - Loads WSGI using a combination of the libraries webtest and paste.deploy;
        - Creates mocked function calls that are necessary to perform the tests.
        :return:
        '''
        # changes db connection to mongo mock
        # builds the test app to respond to requests
        # mock authentication

    def tearDown(self):
        '''
        Method called after the execution of each test. This is used to ensure that both the Database current connection
        and the Mocked methods are proplerly reseted in between the tests.
        :return:
        '''


In each test, a mock object is instantiated this way:


random_application = MockApplicationFactory.build()
random_application.save()


However, the Application entity has a field called "name" setted as required and unique. To attack this constraint, I decided to use 

@factory.post_generation decorator and Factory.FuzzyText to create a new 'name' in each test_case.


Even so, after running the test_cases I still get the error message "Tried to save duplicate unique keys".


I'd like to know how I can generate a random "name" for each mock object so that it is changed before the execution of each test_case.


Declaration of mock object used:


class MockApplicationFactory(factory.Factory):
    """
    Factory class for mock applications
    """

    class Meta:
        model = Application

    credentials = ['test_credential']

    @factory.post_generation
    def random_name(self, create, extracted, **kwargs):
        name = factory.fuzzy.FuzzyText(length=10).fuzz()
Reply all
Reply to author
Forward
0 new messages