@@ -58,7 +58,7 @@ def test_it_reports_a_header_when_set(simpletester):
5858 assert lines == ["Using --randomly-seed=10" ]
5959
6060
61- def test_it_reuses_the_same_random_seed_per_test (ourtester ):
61+ def test_it_uses_different_random_seeds_per_test (ourtester ):
6262 """
6363 Run a pair of tests that generate the a number and then assert they got
6464 what the other did.
@@ -67,18 +67,16 @@ def test_it_reuses_the_same_random_seed_per_test(ourtester):
6767 test_one = """
6868 import random
6969
70+
7071 def test_a():
71- test_a.num = random.random()
72- if hasattr(test_b, 'num'):
73- assert test_a.num == test_b.num
72+ global num
73+ num = random.random()
7474
7575 def test_b():
76- test_b.num = random.random()
77- if hasattr(test_a, 'num'):
78- assert test_b.num == test_a.num
76+ assert random.random() != num
7977 """
8078 )
81- out = ourtester .runpytest ("--randomly-dont-reorganize" )
79+ out = ourtester .runpytest ("--randomly-dont-reorganize" , "--randomly-seed=1" )
8280 out .assert_outcomes (passed = 2 , failed = 0 )
8381
8482
@@ -157,9 +155,8 @@ class A(TestCase):
157155
158156 @classmethod
159157 def setUpClass(cls):
160- super(A, cls ).setUpClass()
158+ super().setUpClass()
161159 cls.suc_num = random.random()
162- assert cls.suc_num == getattr(B, 'suc_num', cls.suc_num)
163160
164161 def test_fake(self):
165162 assert True
@@ -169,15 +166,15 @@ class B(TestCase):
169166
170167 @classmethod
171168 def setUpClass(cls):
172- super(B, cls ).setUpClass()
169+ super().setUpClass()
173170 cls.suc_num = random.random()
174- assert cls.suc_num == getattr(A, 'suc_num', cls .suc_num)
171+ assert cls.suc_num != A .suc_num
175172
176173 def test_fake(self):
177174 assert True
178175 """
179176 )
180- out = ourtester .runpytest ()
177+ out = ourtester .runpytest ("--randomly-seed=1" )
181178 out .assert_outcomes (passed = 2 , failed = 0 )
182179
183180
@@ -195,9 +192,8 @@ def test_fake(self):
195192
196193 @classmethod
197194 def tearDownClass(cls):
198- super(A, cls ).tearDownClass()
195+ super().tearDownClass()
199196 cls.suc_num = random.random()
200- assert cls.suc_num == getattr(B, 'suc_num', cls.suc_num)
201197
202198
203199 class B(TestCase):
@@ -207,12 +203,12 @@ def test_fake(self):
207203
208204 @classmethod
209205 def tearDownClass(cls):
210- super(B, cls ).tearDownClass()
206+ super().tearDownClass()
211207 cls.suc_num = random.random()
212- assert cls.suc_num == getattr(A, 'suc_num', cls .suc_num)
208+ assert cls.suc_num != A .suc_num
213209 """
214210 )
215- out = ourtester .runpytest ()
211+ out = ourtester .runpytest ("--randomly-seed=1" )
216212 out .assert_outcomes (passed = 2 , failed = 0 )
217213
218214
@@ -574,62 +570,20 @@ def test_one(myfixture):
574570 out .assert_outcomes (passed = 1 )
575571
576572
577- def test_fixtures_dont_interfere_with_tests_getting_same_random_state (ourtester ):
578- ourtester .makepyfile (
579- test_one = """
580- import random
581-
582- import pytest
583-
584-
585- random.seed(2)
586- state_at_seed_two = random.getstate()
587-
588-
589- @pytest.fixture(scope='module')
590- def myfixture():
591- return random.random()
592-
593-
594- @pytest.mark.one()
595- def test_one(myfixture):
596- assert random.getstate() == state_at_seed_two
597-
598-
599- @pytest.mark.two()
600- def test_two(myfixture):
601- assert random.getstate() == state_at_seed_two
602- """
603- )
604- args = ["--randomly-seed=2" ]
605-
606- out = ourtester .runpytest (* args )
607- out .assert_outcomes (passed = 2 )
608-
609- out = ourtester .runpytest ("-m" , "one" , * args )
610- out .assert_outcomes (passed = 1 )
611- out = ourtester .runpytest ("-m" , "two" , * args )
612- out .assert_outcomes (passed = 1 )
613-
614-
615573def test_factory_boy (ourtester ):
616574 """
617- Rather than set up factories etc., just check the random generator it uses
618- is set between two tests to output the same number.
575+ Check that the random generator factory boy uses is different between two tests.
619576 """
620577 ourtester .makepyfile (
621578 test_one = """
622579 from factory.random import randgen
623580
624581 def test_a():
625- test_a.num = randgen.random()
626- if hasattr(test_b, 'num'):
627- assert test_a.num == test_b.num
582+ assert randgen.random() == 0.9988532989147809
583+
628584
629585 def test_b():
630- test_b.num = randgen.random()
631- if hasattr(test_a, 'num'):
632- assert test_b.num == test_a.num
586+ assert randgen.random() == 0.18032546798434612
633587 """
634588 )
635589
@@ -645,10 +599,10 @@ def test_faker(ourtester):
645599 fake = Faker()
646600
647601 def test_one():
648- assert fake.name() == 'Ryan Gallagher '
602+ assert fake.name() == 'Mrs. Lisa Ryan '
649603
650604 def test_two():
651- assert fake.name() == 'Ryan Gallagher '
605+ assert fake.name() == 'Kaitlyn Mitchell '
652606 """
653607 )
654608
@@ -660,10 +614,10 @@ def test_faker_fixture(ourtester):
660614 ourtester .makepyfile (
661615 test_one = """
662616 def test_one(faker):
663- assert faker.name() == 'Ryan Gallagher '
617+ assert faker.name() == 'Mrs. Lisa Ryan '
664618
665619 def test_two(faker):
666- assert faker.name() == 'Ryan Gallagher '
620+ assert faker.name() == 'Kaitlyn Mitchell '
667621 """
668622 )
669623
@@ -673,22 +627,17 @@ def test_two(faker):
673627
674628def test_model_bakery (ourtester ):
675629 """
676- Rather than set up models, just check the random generator it uses is set
677- between two tests to output the same number.
630+ Check the Model Bakery random generator is reset between tests.
678631 """
679632 ourtester .makepyfile (
680633 test_one = """
681- from model_bakery.random_gen import baker_random
634+ from model_bakery.random_gen import gen_slug
682635
683636 def test_a():
684- test_a.num = baker_random.random()
685- if hasattr(test_b, 'num'):
686- assert test_a.num == test_b.num
637+ assert gen_slug(10) == 'XjpU5br7ej'
687638
688639 def test_b():
689- test_b.num = baker_random.random()
690- if hasattr(test_a, 'num'):
691- assert test_b.num == test_a.num
640+ assert gen_slug(10) == 'xJHS-PD_WT'
692641 """
693642 )
694643
@@ -702,10 +651,10 @@ def test_numpy(ourtester):
702651 import numpy as np
703652
704653 def test_one():
705- assert np.random.rand() == 0.417022004702574
654+ assert np.random.rand() == 0.36687834264514585
706655
707656 def test_two():
708- assert np.random.rand() == 0.417022004702574
657+ assert np.random.rand() == 0.7050715833365834
709658 """
710659 )
711660
@@ -769,19 +718,19 @@ def fake_entry_points(*, group):
769718 assert reseed .mock_calls == [
770719 mock .call (1 ),
771720 mock .call (1 ),
772- mock .call (0 ),
773- mock .call (1 ),
774- mock .call (2 ),
721+ mock .call (116362448262735926321257785636175308268 ),
722+ mock .call (116362448262735926321257785636175308269 ),
723+ mock .call (116362448262735926321257785636175308270 ),
775724 ]
776725
777726 reseed .mock_calls [:] = []
778727 pytester .runpytest_inprocess ("--randomly-seed=424242" )
779728 assert reseed .mock_calls == [
780729 mock .call (424242 ),
781730 mock .call (424242 ),
782- mock .call (424241 ),
783- mock .call (424242 ),
784- mock .call (424243 ),
731+ mock .call (116362448262735926321257785636175732509 ),
732+ mock .call (116362448262735926321257785636175732510 ),
733+ mock .call (116362448262735926321257785636175732511 ),
785734 ]
786735
787736
0 commit comments