File: test_thread_unsafe_detection.py

package info (click to toggle)
python-pytest-run-parallel 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 320 kB
  • sloc: python: 1,919; makefile: 3
file content (762 lines) | stat: -rw-r--r-- 22,577 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import os
import sys
import textwrap

import pytest

try:
    import hypothesis
except ImportError:
    hypothesis = None


WARNINGS_IS_THREADSAFE = bool(
    getattr(sys.flags, "context_aware_warnings", 0)
    and getattr(sys.flags, "thread_inherit_context", 0)
)
WARNINGS_PASS = "PARALLEL " if WARNINGS_IS_THREADSAFE else ""
CTYPES_PASS = "PARALLEL " if sys.version_info > (3, 13) else ""


def test_thread_unsafe_marker(pytester):
    # create a temporary pytest test module
    pytester.makepyfile("""
        import pytest

        @pytest.mark.thread_unsafe
        def test_should_run_single(num_parallel_threads):
            assert num_parallel_threads == 1

        @pytest.mark.thread_unsafe(reason='this is thread-unsafe')
        def test_should_run_single_2(num_parallel_threads):
            assert num_parallel_threads == 1
    """)

    # run pytest with the following cmd args
    result = pytester.runpytest("--parallel-threads=10", "-v")

    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(
        [
            "*::test_should_run_single PASSED*",
            "*::test_should_run_single_2 PASSED *thread-unsafe*: this is thread-unsafe*",
            "*2 tests were not run in parallel*",
        ]
    )

    # check that skipping works too
    result = pytester.runpytest(
        "--parallel-threads=10", "--skip-thread-unsafe=True", "-v"
    )

    result.stdout.fnmatch_lines(
        [
            "*::test_should_run_single SKIPPED*",
            "*::test_should_run_single_2 SKIPPED*",
            "*2 tests were skipped*",
        ]
    )

    result.stdout.no_fnmatch_line("*All tests were run in parallel*")


def test_pytest_warns_detection(pytester):
    # create a temporary pytest test module
    pytester.makepyfile("""
        import pytest
        import warnings
        import pytest as pyt
        import warnings as w
        import sys
        from pytest import warns, deprecated_call
        from warnings import catch_warnings

        warns_alias = warns

        WARNINGS_IS_THREADSAFE = (
            getattr(sys.flags, "context_aware_warnings", 0) and
            getattr(sys.flags, "thread_inherit_context", 0))

        def test_single_thread_warns_1(num_parallel_threads):
            with pytest.warns(UserWarning):
                warnings.warn('example', UserWarning)
            if WARNINGS_IS_THREADSAFE:
                assert num_parallel_threads == 10
            else:
                assert num_parallel_threads == 1

        def test_single_thread_warns_2(num_parallel_threads):
            with warns(UserWarning):
                warnings.warn('example', UserWarning)
            if WARNINGS_IS_THREADSAFE:
                assert num_parallel_threads == 10
            else:
                assert num_parallel_threads == 1

        def test_single_thread_warns_3(num_parallel_threads):
            with pyt.warns(UserWarning):
                warnings.warn('example', UserWarning)
            if WARNINGS_IS_THREADSAFE:
                assert num_parallel_threads == 10
            else:
                assert num_parallel_threads == 1

        def test_single_thread_warns_4(num_parallel_threads):
            with warns_alias(UserWarning):
                warnings.warn('example', UserWarning)
            if WARNINGS_IS_THREADSAFE:
                assert num_parallel_threads == 10
            else:
                assert num_parallel_threads == 1
    """)

    # run pytest with the following cmd args
    result = pytester.runpytest("--parallel-threads=10", "-v")

    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(
        [
            f"*::test_single_thread_warns_1 {WARNINGS_PASS}PASSED*",
            f"*::test_single_thread_warns_2 {WARNINGS_PASS}PASSED*",
            f"*::test_single_thread_warns_3 {WARNINGS_PASS}PASSED*",
            f"*::test_single_thread_warns_4 {WARNINGS_PASS}PASSED*",
        ]
    )

    if not (
        getattr(sys.flags, "context_aware_warnings", 0)
        or getattr(sys.flags, "thread_inherit_context", 0)
    ):
        # check that skipping works too
        result = pytester.runpytest(
            "--parallel-threads=10", "--skip-thread-unsafe=True", "-v"
        )

        result.stdout.fnmatch_lines(
            [
                "*::test_single_thread_warns_1 SKIPPED*",
                "*::test_single_thread_warns_2 SKIPPED*",
                "*::test_single_thread_warns_3 SKIPPED*",
                "*::test_single_thread_warns_4 SKIPPED*",
            ]
        )


def test_warns_detection_config_option(pytester):
    # create a temporary pytest test module
    pytester.makepyfile("""
        import pytest
        import warnings
        import pytest as pyt
        import warnings as w
        import sys
        from pytest import warns, deprecated_call
        from warnings import catch_warnings

        warns_alias = warns

        def test_single_thread_warns_1(num_parallel_threads):
            with pytest.warns(UserWarning):
                warnings.warn('example', UserWarning)
            assert num_parallel_threads == 1

        def test_single_thread_warns_2(num_parallel_threads):
            with warns(UserWarning):
                warnings.warn('example', UserWarning)
            assert num_parallel_threads == 1

        def test_single_thread_warns_3(num_parallel_threads):
            with pyt.warns(UserWarning):
                warnings.warn('example', UserWarning)
            assert num_parallel_threads == 1

        def test_single_thread_warns_4(num_parallel_threads):
            with warns_alias(UserWarning):
                warnings.warn('example', UserWarning)
            assert num_parallel_threads == 1

    """)

    # run pytest with the following cmd args
    result = pytester.runpytest(
        "--parallel-threads=10", "-v", "--mark-warnings-as-unsafe"
    )

    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(
        [
            "*::test_single_thread_warns_1 PASSED*",
            "*::test_single_thread_warns_2 PASSED*",
            "*::test_single_thread_warns_3 PASSED*",
            "*::test_single_thread_warns_4 PASSED*",
        ]
    )

    # check that skipping works too
    result = pytester.runpytest(
        "--parallel-threads=10",
        "--skip-thread-unsafe=True",
        "-v",
        "--mark-warnings-as-unsafe",
    )

    result.stdout.fnmatch_lines(
        [
            "*::test_single_thread_warns_1 SKIPPED*",
            "*::test_single_thread_warns_2 SKIPPED*",
            "*::test_single_thread_warns_3 SKIPPED*",
            "*::test_single_thread_warns_4 SKIPPED*",
        ]
    )


def test_thread_unsafe_fixtures(pytester):
    # create a temporary pytest test module
    pytester.makepyfile("""
        import sys

        import pytest

        @pytest.fixture
        def my_unsafe_fixture():
            pass

        @pytest.fixture
        def my_unsafe_fixture_2():
            pass

        WARNINGS_IS_THREADSAFE = (
            getattr(sys.flags, "context_aware_warnings", 0) and
            getattr(sys.flags, "thread_inherit_context", 0))

        def test_capsys(capsys, num_parallel_threads):
            assert num_parallel_threads == 1

        def test_monkeypatch(monkeypatch, num_parallel_threads):
            assert num_parallel_threads == 1

        def test_recwarn(recwarn, num_parallel_threads):
            if WARNINGS_IS_THREADSAFE:
                assert num_parallel_threads == 10
            else:
                assert num_parallel_threads == 1

        def test_custom_fixture_skip(my_unsafe_fixture, num_parallel_threads):
            assert num_parallel_threads == 1

        def test_custom_fixture_skip_2(my_unsafe_fixture_2, num_parallel_threads):
            assert num_parallel_threads == 1
    """)

    pytester.makeini("""
    [pytest]
    thread_unsafe_fixtures =
        my_unsafe_fixture
        my_unsafe_fixture_2
    """)

    # run pytest with the following cmd args
    result = pytester.runpytest("--parallel-threads=10", "-v")

    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(
        [
            "*::test_capsys PASSED *thread-unsafe*: uses thread-unsafe fixture*",
            f"*::test_recwarn {WARNINGS_PASS}PASSED*",
            "*::test_custom_fixture_skip PASSED *thread-unsafe*: uses thread-unsafe fixture*",
            "*::test_custom_fixture_skip_2 PASSED *thread-unsafe*: uses thread-unsafe fixture*",
        ]
    )


def test_thread_unsafe_function_attr(pytester):
    pytester.makepyfile(
        mod_1="""
        def to_skip():
            __thread_safe__ = False

        def not_to_skip():
            __thread_safe__ = True
    """
    )

    pytester.makepyfile(
        mod_2="""
        import mod_1
        from mod_1 import not_to_skip

        def some_fn_calls_skip():
            mod_1.to_skip()

        def some_fn_should_not_skip():
            not_to_skip()

        def marked_for_skip():
            pass
    """
    )

    pytester.makepyfile("""
        import mod_2
        from mod_2 import some_fn_calls_skip

        def test_should_be_marked_1(num_parallel_threads):
            mod_2.some_fn_calls_skip()
            assert num_parallel_threads == 1

        def test_should_not_be_marked(num_parallel_threads):
            mod_2.some_fn_should_not_skip()
            assert num_parallel_threads == 10

        def test_should_be_marked_2(num_parallel_threads):
            mod_2.marked_for_skip()
            assert num_parallel_threads == 1

        def test_should_be_marked_3(num_parallel_threads):
            some_fn_calls_skip()
            assert num_parallel_threads == 1
    """)

    pytester.makeini("""
    [pytest]
    thread_unsafe_functions =
        mod_2.marked_for_skip
    """)

    # run pytest with the following cmd args
    orig = os.environ.get("PYTEST_RUN_PARALLEL_VERBOSE", "0")
    os.environ["PYTEST_RUN_PARALLEL_VERBOSE"] = "0"
    result = pytester.runpytest("--parallel-threads=10", "-v")

    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines(
        [
            "*Collected 1 items to run in parallel*",
            "*::test_should_be_marked_1 PASSED *thread-unsafe*inferred via func.__thread_safe__*",
            "*::test_should_not_be_marked PARALLEL PASSED*",
            "*::test_should_be_marked_2 PASSED *thread-unsafe*marked_for_skip*",
            "*::test_should_be_marked_3 PASSED *thread-unsafe*inferred via func.__thread_safe__*",
        ]
    )

    result.stdout.fnmatch_lines(
        [
            "*3 tests were not run in parallel because of use of thread-unsafe "
            "functionality, to list the tests that were not run in parallel, "
            "re-run while setting PYTEST_RUN_PARALLEL_VERBOSE=1 in your "
            "shell environment*",
        ]
    )

    # re-run with PYTEST_RUN_PARALLEL_VERBOSE=1
    os.environ["PYTEST_RUN_PARALLEL_VERBOSE"] = "1"
    result = pytester.runpytest("--parallel-threads=10", "-v")
    os.environ["PYTEST_RUN_PARALLEL_VERBOSE"] = orig

    result.stdout.fnmatch_lines(
        [
            "*Collected 1 items to run in parallel*",
            "*::test_should_be_marked_1 PASSED *thread-unsafe*: calls thread-unsafe function*",
            "*::test_should_not_be_marked PARALLEL PASSED*",
            "*::test_should_be_marked_2 PASSED*",
            "*::test_should_be_marked_3 PASSED*",
            "*::test_should_be_marked_1*",
            "*::test_should_be_marked_2*",
            "*::test_should_be_marked_3*",
        ]
    )


def test_detect_unittest_mock(pytester):
    pytester.makepyfile("""
    import sys
    from unittest import mock

    @mock.patch("sys.platform", "VAX")
    def test_uses_mock(num_parallel_threads):
        assert sys.platform == "VAX"
        assert num_parallel_threads == 1
    """)
    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            r"*::test_uses_mock PASSED*" r"calls thread-unsafe function: mock.patch*",
        ]
    )


def test_recurse_assign(pytester):
    pytester.makepyfile("""
    import pytest

    def unsafe():
        __thread_safe__ = False

    def test_function_recurse_on_assign(num_parallel_threads):
        w = unsafe()
        assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            "*::test_function_recurse_on_assign PASSED*",
        ]
    )


def test_failed_thread_unsafe(pytester):
    pytester.makepyfile("""
    import pytest

    @pytest.mark.thread_unsafe
    def test1():
        assert False
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    assert result.ret == 1
    print(result.stdout)
    result.stdout.fnmatch_lines(
        [
            "*::test1 FAILED *thread-unsafe*: uses the thread_unsafe marker*",
            "* FAILURES *",
            "*1 failed*",
        ]
    )


def test_chained_attribute_import(pytester):
    pytester.makepyfile("""
    import _pytest.recwarn
    import sys

    WARNINGS_IS_THREADSAFE = (
        getattr(sys.flags, "context_aware_warnings", 0) and
        getattr(sys.flags, "thread_inherit_context", 0))

    def test_chained_attribute_thread_unsafe_detection(num_parallel_threads):
        _pytest.recwarn.warns()
        if WARNINGS_IS_THREADSAFE:
            assert num_parallel_threads == 10
        else:
            assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            f"*::test_chained_attribute_thread_unsafe_detection {WARNINGS_PASS}PASSED*",
        ]
    )


def test_chained_attribute_thread_safe_assignment(pytester):
    pytester.mkpydir("mod")
    file = pytester.path / "mod" / "submod.py"
    file.write_text(
        textwrap.dedent("""
    def to_skip():
        __thread_safe__ = False
    """)
    )
    pytester.makepyfile("""
    import mod.submod

    def test_chained_attribute_thread_safe_assignment(num_parallel_threads):
        mod.submod.to_skip()
        assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            "*::test_chained_attribute_thread_safe_assignment PASSED*",
        ]
    )


def test_wrapped_function_call(pytester):
    pytester.makepyfile("""
    def unsafe(x):
        __thread_safe__ = False
        return x

    def wrapper(x):
        return x

    def test_wrapped_function_call(num_parallel_threads):
        wrapper(unsafe(1))
        assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            "*::test_wrapped_function_call PASSED*",
        ]
    )


def test_thread_unsafe_function_call_in_assignment(pytester):
    pytester.makepyfile("""
    def unsafe():
        __thread_safe__ = False
        return 1

    def test_thread_unsafe_function_call_in_assignment(num_parallel_threads):
        x = y = unsafe()
        assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            "*::test_thread_unsafe_function_call_in_assignment PASSED*",
        ]
    )


def test_thread_unsafe_unittest_mock_patch_object(pytester):
    pytester.makepyfile("""
    import sys
    import unittest.mock

    @unittest.mock.patch.object(sys, "platform", "VAX")
    def test_thread_unsafe_unittest_mock_patch_object(num_parallel_threads):
        assert sys.platform == "VAX"
        assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            "*::test_thread_unsafe_unittest_mock_patch_object PASSED*",
        ]
    )


def test_thread_unsafe_ctypes(pytester):
    pytester.makepyfile("""
    import ctypes.util
    import sys

    CTYPES_IS_THREADSAFE = sys.version_info > (3, 13)

    def test_thread_unsafe_ctypes(num_parallel_threads):
        ctypes.util.find_library("m")
        if CTYPES_IS_THREADSAFE:
            assert num_parallel_threads == 10
        else:
            assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            f"*::test_thread_unsafe_ctypes {CTYPES_PASS}PASSED*",
        ]
    )


def test_thread_unsafe_ctypes_config_option(pytester):
    pytester.makepyfile("""
    import ctypes.util
    import sys

    def test_thread_unsafe_ctypes(num_parallel_threads):
        ctypes.util.find_library("m")
        assert num_parallel_threads == 1
    """)

    result = pytester.runpytest(
        "--parallel-threads=10", "-v", "--mark-ctypes-as-unsafe"
    )
    result.stdout.fnmatch_lines(
        [
            "*::test_thread_unsafe_ctypes PASSED*",
        ]
    )


def test_thread_unsafe_ctypes_import_from(pytester):
    pytester.makepyfile("""
    import sys
    from ctypes.util import find_library

    CTYPES_IS_THREADSAFE = sys.version_info > (3, 13)

    def test_thread_unsafe_ctypes(num_parallel_threads):
        find_library("m")
        if CTYPES_IS_THREADSAFE:
            assert num_parallel_threads == 10
        else:
            assert num_parallel_threads == 1

    def test_thread_unsafe_not_using_ctypes(num_parallel_threads):
        assert num_parallel_threads == 10
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            f"*::test_thread_unsafe_ctypes {CTYPES_PASS}PASSED*",
            "*::test_thread_unsafe_not_using_ctypes PARALLEL PASSED*",
        ]
    )


def test_thread_unsafe_ctypes_import_from_config_option(pytester):
    pytester.makepyfile("""
    import sys
    from ctypes.util import find_library

    def test_thread_unsafe_ctypes(num_parallel_threads):
        find_library("m")
        assert num_parallel_threads == 1

    def test_thread_unsafe_not_using_ctypes(num_parallel_threads):
        assert num_parallel_threads == 10
    """)

    result = pytester.runpytest(
        "--parallel-threads=10", "-v", "--mark-ctypes-as-unsafe"
    )
    result.stdout.fnmatch_lines(
        [
            "*::test_thread_unsafe_ctypes PASSED*",
            "*::test_thread_unsafe_not_using_ctypes PARALLEL PASSED*",
        ]
    )


def test_thread_unsafe_pytest_warns_multiline_string(pytester):
    pytester.makepyfile("""
import sys
import warnings

import pytest

WARNINGS_IS_THREADSAFE = (
    getattr(sys.flags, "context_aware_warnings", 0) and
    getattr(sys.flags, "thread_inherit_context", 0))

class TestThreadUnsafePytestWarnsMultilineString:
    def test_thread_unsafe_pytest_warns_multiline_string1(self, num_parallel_threads):
        with pytest.warns(UserWarning) as r:
            warnings.warn("foo", UserWarning)
        '''
Hello world'''
        if WARNINGS_IS_THREADSAFE:
            assert num_parallel_threads == 10
        else:
            assert num_parallel_threads == 1

    def test_thread_unsafe_pytest_warns_multiline_string2(self, num_parallel_threads):
        with pytest.warns(UserWarning) as r:
            warnings.warn("foo", UserWarning)
        '''
Hello world'''
        if WARNINGS_IS_THREADSAFE:
            assert num_parallel_threads == 10
        else:
            assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            f"*::test_thread_unsafe_pytest_warns_multiline_string1 {WARNINGS_PASS}PASSED*",
            f"*::test_thread_unsafe_pytest_warns_multiline_string2 {WARNINGS_PASS}PASSED*",
        ]
    )


def test_thread_unsafe_pytest_warns_instance_decorator(pytester):
    pytester.makepyfile("""
import sys
import warnings

import pytest

WARNINGS_IS_THREADSAFE = (
    getattr(sys.flags, "context_aware_warnings", 0) and
    getattr(sys.flags, "thread_inherit_context", 0))


def identity(func):
    # Decorator that does nothing
    return func


class TestThreadUnsafePytestWarnsInstanceDecorator:
    @identity
    def test_thread_unsafe_pytest_warns_instance_decorator(self, num_parallel_threads):
        with pytest.warns(UserWarning) as r:
            warnings.warn("foo", UserWarning)
        if WARNINGS_IS_THREADSAFE:
            assert num_parallel_threads == 10
        else:
            assert num_parallel_threads == 1
    @identity
    def test_thread_unsafe_pytest_warns_instance_decorator_with_multiline(self, num_parallel_threads):
        with pytest.warns(UserWarning) as r:
            warnings.warn("foo", UserWarning)
        '''foo
'''
        if WARNINGS_IS_THREADSAFE:
            assert num_parallel_threads == 10
        else:
            assert num_parallel_threads == 1
    """)

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            f"*::test_thread_unsafe_pytest_warns_instance_decorator {WARNINGS_PASS}PASSED*",
            f"*::test_thread_unsafe_pytest_warns_instance_decorator_with_multiline {WARNINGS_PASS}PASSED*",
        ]
    )


@pytest.mark.skipif(hypothesis is None, reason="hypothesis needs to be installed")
def test_thread_unsafe_hypothesis_config_option(pytester):
    pytester.makepyfile("""
    from hypothesis import given, strategies as st, settings, HealthCheck

    @given(st.integers())
    @settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
    def test_thread_unsafe_hypothesis(num_parallel_threads, n):
        assert num_parallel_threads == 1
        assert isinstance(n, int)
    """)

    result = pytester.runpytest(
        "--parallel-threads=10", "-v", "--mark-hypothesis-as-unsafe"
    )
    result.stdout.fnmatch_lines(
        [
            "*::test_thread_unsafe_hypothesis PASSED*",
        ]
    )


def test_thread_unsafe_detection_can_handle_none_module(pytester):
    pytester.makepyfile(
        """
    global_lambda = eval("lambda x: x + 1", {})

    def test_has_l_in_globals(num_parallel_threads):
        assert num_parallel_threads == 10

    assert global_lambda.__module__ is None
    assert "global_lambda" in test_has_l_in_globals.__globals__
    """
    )

    result = pytester.runpytest("--parallel-threads=10", "-v")
    result.stdout.fnmatch_lines(
        [
            "*::test_has_l_in_globals PARALLEL PASSED*",
        ]
    )