Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

A Nose plugin that seeds the faker package.

Project description

nose-seed-faker

A nose plugin for seeding the faker package based on test names.

$ nosetests --with-seed-faker mytests/

Faker is seeded at the beginning of each test based on the test name, so each test that uses faker will use the same fake data between test runs, regardless of test order.

This relies on the behavior that faker seeds a instance of random that is shared globally across instances of the Faker class, so seeding any instance of Faker will seed every instance of Faker.

For example, these two tests use faker to generate fake email addresses:

import unittest

from faker import Faker

fake = Faker()


class TestEmail(unittest.TestCase):
    def test_one(self):
        email = fake.email()
        print(email)

    def test_two(self):
        email = fake.email()
        print(email)

Since faker generates random email addresses, if we run test_one multiple times, we’ll use different emails for each test run:

$ nosetests -v --nocapture tests.py:TestEmail.test_one tests.py:TestEmail.test_one
test_one (tests.TestEmail) ... opouros@hermistondeckow.com
ok
test_one (tests.TestEmail) ... sgrady@hotmail.com
ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

While this is good for increasing the surface area of emails we use in our tests, it can cause flaky tests – a test could mysteriously fail, but if you tried running it again to debug the failure, all of a sudden it’s passing again!

nose-seed-faker solves this problem by seeding faker based on the name of each test. Using the plugin, if you run the same test multiple times, it’ll use the same email address:

$ nosetests --with-seed-faker -v --nocapture tests.py:TestEmail.test_one tests.py:TestEmail.test_one
test_one (tests.TestEmail) ... mitchel07@yost.com
ok
test_one (tests.TestEmail) ... mitchel07@yost.com
ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

Each individual test will use a different email, so we still get a good surface area of emails to use in our tests:

$ nosetests --with-seed-faker -v --nocapture tests.py
test_one (tests.TestEmail) ... mitchel07@yost.com
ok
test_two (tests.TestEmail) ... kurt.bartell@yahoo.com
ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

Installation

$ pip install nose-seed-faker

Warning

Versions 0.5.2 and lower of faker will seed the random module globally.

Version 0.5.3 fixes this issue by seeding an instance of the random.Random class, but at the time of this writing, this fix hasn’t been released to PyPI yet.

Until version 0.5.3 is released on PyPI, you can install it like so:

$ pip install git+http://github.com/joke2k/faker.git@dabf922af72783ebd9d163611ab0f4f307156443#egg=fake-factory==0.5.3

History

0.1.2

released 2015-06-30

  • initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
nose-seed-faker-0.1.2.macosx-10.9-x86_64.exe (69.6 kB) Copy SHA256 hash SHA256 Windows Installer any
nose-seed-faker-0.1.2.tar.gz (4.1 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page