From 617c6478394eadeaf2516863bd920781f4c91e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20L=C3=A9tendart?= Date: Wed, 27 May 2020 13:24:18 +0200 Subject: [PATCH] Add faker and faker_seed fixtures support (#261) Co-authored-by: Adam Johnson --- HISTORY.rst | 7 +++++++ README.rst | 15 +++++++++------ src/pytest_randomly.py | 9 ++++++++- tests/test_pytest_randomly.py | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 7ce94ae..e4b8838 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,13 @@ History ------- +* Provide a ``faker_seed`` fixture to set the seed for tests using faker's + pytest fixtures (as per its + `docs `__). + + Thanks to Romain Létendart for the change in `PR #261 + `__. + 3.3.1 (2020-04-15) ------------------ diff --git a/README.rst b/README.rst index f09b463..8ec6c12 100644 --- a/README.rst +++ b/README.rst @@ -37,7 +37,10 @@ All of these features are on by default but can be disabled with flags. allows for repeatable use of its random 'fuzzy' features. * If `faker `_ is installed, its random state is reset at the start of every test. This is also for repeatable fuzzy - data in tests - factory boy uses faker for lots of data. + data in tests - factory boy uses faker for lots of data. This is also done + if you're using the ``faker`` pytest fixture, by defining the ``faker_seed`` + fixture + (`docs `__). * If `numpy `_ is installed, its random state is reset at the start of every test. * If additional random generators are used, they can be registered under the @@ -57,11 +60,11 @@ tests themselves, as well as giving a little more coverage to your system. By randomly ordering the tests, the risk of surprising inter-test dependencies is reduced - a technique used in many places, for example Google's C++ test runner `googletest -`_. -Research suggests that "dependent tests do exist in practice" and a random -order of test executions can effectively detect such dependencies [1]_. -Alternatively, a reverse order of test executions, as provided by `pytest-reverse -`__, may find less dependent +`_. +Research suggests that "dependent tests do exist in practice" and a random +order of test executions can effectively detect such dependencies [1]_. +Alternatively, a reverse order of test executions, as provided by `pytest-reverse +`__, may find less dependent tests but can achieve a better benefit/cost ratio. By resetting the random seed to a repeatable number for each test, tests can diff --git a/src/pytest_randomly.py b/src/pytest_randomly.py index 5c0b8fa..47523d3 100644 --- a/src/pytest_randomly.py +++ b/src/pytest_randomly.py @@ -3,7 +3,7 @@ import sys import time -from pytest import Collector +from pytest import Collector, fixture if sys.version_info >= (3, 8): from importlib.metadata import entry_points @@ -239,3 +239,10 @@ def reduce_list_of_lists(lists): for list_ in lists: new_list.extend(list_) return new_list + + +if have_faker: + + @fixture(autouse=True) + def faker_seed(pytestconfig): + return pytestconfig.getoption("randomly_seed") diff --git a/tests/test_pytest_randomly.py b/tests/test_pytest_randomly.py index a44c9fa..ee01e02 100644 --- a/tests/test_pytest_randomly.py +++ b/tests/test_pytest_randomly.py @@ -593,6 +593,21 @@ def test_two(): out.assert_outcomes(passed=2) +def test_faker_fixture(ourtestdir): + ourtestdir.makepyfile( + test_one=""" + def test_one(faker): + assert faker.name() == 'Ryan Gallagher' + + def test_two(faker): + assert faker.name() == 'Ryan Gallagher' + """ + ) + + out = ourtestdir.runpytest("--randomly-seed=1") + out.assert_outcomes(passed=2) + + def test_numpy(ourtestdir): ourtestdir.makepyfile( test_one="""