Skip to main content

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.

Source Distribution

nose-seed-faker-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nose-seed-faker-0.1.2.macosx-10.9-x86_64.exe (69.6 kB view details)

Uploaded Source

File details

Details for the file nose-seed-faker-0.1.2.tar.gz.

File metadata

File hashes

Hashes for nose-seed-faker-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5721afc2838c478ae5ad8550d3716353bfd4cc7f10a71f5214c9ccc53ed78a5e
MD5 e9b6c5ab75fe3ac17193fa54881dd94f
BLAKE2b-256 46948cbe8d992d54059be6ab5e99fb77970159e3a5ea450a7a7d28f5f200cc17

See more details on using hashes here.

File details

Details for the file nose-seed-faker-0.1.2.macosx-10.9-x86_64.exe.

File metadata

File hashes

Hashes for nose-seed-faker-0.1.2.macosx-10.9-x86_64.exe
Algorithm Hash digest
SHA256 89f2be74076d106b7403472ac238202da0b1e272656acf2fc36e8a47280b1231
MD5 bb7565bcecdfcd0b978b342644ad4c93
BLAKE2b-256 13726a5d025d60e91f2b6a814d9e18942afa31e472de3a3853ed2144c13b0ed4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page