File: test_plugin.py

package info (click to toggle)
pytest-httpx 0.35.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 424 kB
  • sloc: python: 4,386; makefile: 7; sh: 5
file content (743 lines) | stat: -rw-r--r-- 27,105 bytes parent folder | download
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
from pytest import Testdir


def test_fixture_is_available(testdir: Testdir) -> None:
    # create a temporary pytest test file
    testdir.makepyfile(
        """
        import httpx
        
        
        def test_http(httpx_mock):
            mock = httpx_mock.add_response(url="https://foo.tld")
            r = httpx.get("https://foo.tld")
            assert httpx_mock.get_request() is not None

    """
    )

    # run all tests with pytest
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_httpx_mock_unused_response(testdir: Testdir) -> None:
    """
    Unused responses should fail test case.
    """
    testdir.makepyfile(
        """
        def test_httpx_mock_unused_response(httpx_mock):
            httpx_mock.add_response()
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, passed=1)
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match any request",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )


def test_httpx_mock_unused_response_without_assertion(testdir: Testdir) -> None:
    """
    Unused responses should not fail test case if
    assert_all_responses_were_requested option is set to False.
    """
    testdir.makepyfile(
        """
        import pytest

        @pytest.mark.httpx_mock(assert_all_responses_were_requested=False)
        def test_httpx_mock_unused_response_without_assertion(httpx_mock):
            httpx_mock.add_response()
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_httpx_mock_unused_callback(testdir: Testdir) -> None:
    """
    Unused callbacks should fail test case.
    """
    testdir.makepyfile(
        """
        def test_httpx_mock_unused_callback(httpx_mock):
            def unused(*args, **kwargs):
                pass
        
            httpx_mock.add_callback(unused)

    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, passed=1)
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match any request",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )


def test_httpx_mock_unused_callback_without_assertion(testdir: Testdir) -> None:
    """
    Unused callbacks should not fail test case if
    assert_all_responses_were_requested option is set to False.
    """
    testdir.makepyfile(
        """
        import pytest

        @pytest.mark.httpx_mock(assert_all_responses_were_requested=False)
        def test_httpx_mock_unused_callback_without_assertion(httpx_mock):
            def unused(*args, **kwargs):
                pass
        
            httpx_mock.add_callback(unused)

    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_httpx_mock_unexpected_request(testdir: Testdir) -> None:
    """
    Unexpected request should fail test case if
    assert_all_requests_were_expected option is set to True (default).
    """
    testdir.makepyfile(
        """
        import httpx
        import pytest

        def test_httpx_mock_unexpected_request(httpx_mock):
            with httpx.Client() as client:
                # Non mocked request
                with pytest.raises(httpx.TimeoutException):
                    client.get("https://foo.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, passed=1)
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following requests were not expected:",
            "*  - GET request on https://foo.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-not-register-responses-for-every-request",
        ],
        consecutive=True,
    )


def test_httpx_mock_unexpected_request_without_assertion(testdir: Testdir) -> None:
    """
    Unexpected request should not fail test case if
    assert_all_requests_were_expected option is set to False.
    """
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.httpx_mock(assert_all_requests_were_expected=False)
        def test_httpx_mock_unexpected_request(httpx_mock):
            with httpx.Client() as client:
                # Non mocked request
                with pytest.raises(httpx.TimeoutException):
                    client.get("https://foo.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_httpx_mock_already_matched_response(testdir: Testdir) -> None:
    """
    Already matched response should fail test case if
    can_send_already_matched_responses option is set to False (default).
    """
    testdir.makepyfile(
        """
        import httpx
        import pytest

        def test_httpx_mock_already_matched_response(httpx_mock):
            httpx_mock.add_response()
            with httpx.Client() as client:
                client.get("https://foo.tld")
                # Non mocked (already matched) request
                with pytest.raises(httpx.TimeoutException):
                    client.get("https://foo.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, passed=1)
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following requests were not expected:",
            "*  - GET request on https://foo.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-not-register-responses-for-every-request",
        ],
        consecutive=True,
    )


def test_httpx_mock_reusing_matched_response(testdir: Testdir) -> None:
    """
    Already matched response should not fail test case if
    can_send_already_matched_responses option is set to True.
    """
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.httpx_mock(can_send_already_matched_responses=True)
        def test_httpx_mock_reusing_matched_response(httpx_mock):
            httpx_mock.add_response()
            with httpx.Client() as client:
                client.get("https://foo.tld")
                # Reusing response
                client.get("https://foo.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_httpx_mock_unmatched_request_without_responses(
    testdir: Testdir,
) -> None:
    testdir.makepyfile(
        """
        import httpx
        import pytest

        def test_httpx_mock_unmatched_request_without_responses(httpx_mock):
            with httpx.Client() as client:
                # This request will not be matched
                client.get("https://foo22.tld")
                # This code will not be reached
                client.get("https://foo3.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, failed=1)
    # Assert the error that occurred
    result.stdout.fnmatch_lines(
        [
            "*httpx.TimeoutException: No response can be found for GET request on https://foo22.tld",
        ],
        consecutive=True,
    )
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following requests were not expected:",
            "*  - GET request on https://foo22.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-not-register-responses-for-every-request",
        ],
        consecutive=True,
    )


def test_httpx_mock_unmatched_request_with_only_unmatched_responses(
    testdir: Testdir,
) -> None:
    testdir.makepyfile(
        """
        import httpx
        import pytest

        def test_httpx_mock_unmatched_request_with_only_unmatched_responses(httpx_mock):
            # This response will not be sent (because of a typo in the URL)
            httpx_mock.add_response(url="https://foo2.tld")
            # This response will not be sent (because test execution failed earlier)
            httpx_mock.add_response(url="https://foo3.tld")
            
            with httpx.Client() as client:
                # This request will not be matched
                client.get("https://foo22.tld")
                # This code will not be reached
                client.get("https://foo3.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, failed=1)
    # Assert the error that occurred
    result.stdout.fnmatch_lines(
        [
            "*httpx.TimeoutException: No response can be found for GET request on https://foo22.tld amongst:",
            "*- Match any request on https://foo2.tld",
            "*- Match any request on https://foo3.tld",
        ],
        consecutive=True,
    )
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match any request on https://foo2.tld",
            "*  - Match any request on https://foo3.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )


def test_httpx_mock_unmatched_request_with_only_unmatched_reusable_responses(
    testdir: Testdir,
) -> None:
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.httpx_mock(can_send_already_matched_responses=True)
        def test_httpx_mock_unmatched_request_with_only_unmatched_responses(httpx_mock):
            # This response will not be sent (because of a typo in the URL)
            httpx_mock.add_response(url="https://foo2.tld", method="GET")
            # This response will not be sent (because test execution failed earlier)
            httpx_mock.add_response(url="https://foo3.tld")
            
            with httpx.Client() as client:
                # This request will not be matched
                client.get("https://foo22.tld")
                # This code will not be reached
                client.get("https://foo3.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, failed=1)
    # Assert the error that occurred
    result.stdout.fnmatch_lines(
        [
            "*httpx.TimeoutException: No response can be found for GET request on https://foo22.tld amongst:",
            "*- Match GET request on https://foo2.tld",
            "*- Match every request on https://foo3.tld",
        ],
        consecutive=True,
    )
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match GET request on https://foo2.tld",
            "*  - Match every request on https://foo3.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )


def test_httpx_mock_unmatched_request_with_only_matched_responses(
    testdir: Testdir,
) -> None:
    testdir.makepyfile(
        """
        import httpx
        import pytest

        def test_httpx_mock_unmatched_request_with_only_matched_responses(httpx_mock):
            # Sent response
            httpx_mock.add_response(url="https://foo.tld")
            # Sent response
            httpx_mock.add_response(url="https://foo.tld")
            
            with httpx.Client() as client:
                client.get("https://foo.tld")
                client.get("https://foo.tld")
                # This request will not be matched
                client.get("https://foo22.tld")
                # This code will not be reached
                client.get("https://foo3.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, failed=1)
    # Assert the error that occurred
    result.stdout.fnmatch_lines(
        [
            "*httpx.TimeoutException: No response can be found for GET request on https://foo22.tld amongst:",
            "*- Already matched any request on https://foo.tld",
            "*- Already matched any request on https://foo.tld",
            "*",
            "*If you wanted to reuse an already matched response instead of registering it again, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-a-response-for-more-than-one-request",
        ],
        consecutive=True,
    )
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following requests were not expected:",
            "*  - GET request on https://foo22.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-not-register-responses-for-every-request",
        ],
        consecutive=True,
    )


def test_httpx_mock_unmatched_request_with_only_matched_reusable_responses(
    testdir: Testdir,
) -> None:
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.httpx_mock(can_send_already_matched_responses=True)
        def test_httpx_mock_unmatched_request_with_only_matched_responses(httpx_mock):
            # Sent response
            httpx_mock.add_response(url="https://foo.tld")
            # Sent response
            httpx_mock.add_response(url="https://foo3.tld")
            
            with httpx.Client() as client:
                client.get("https://foo.tld")
                client.get("https://foo.tld")
                client.get("https://foo3.tld")
                # This request will not be matched
                client.get("https://foo22.tld")
                # This code will not be reached
                client.get("https://foo3.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, failed=1)
    # Assert the error that occurred
    result.stdout.fnmatch_lines(
        [
            "*httpx.TimeoutException: No response can be found for GET request on https://foo22.tld amongst:",
            "*- Match every request on https://foo.tld",
            "*- Match every request on https://foo3.tld",
        ],
        consecutive=True,
    )
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following requests were not expected:",
            "*  - GET request on https://foo22.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-not-register-responses-for-every-request",
        ],
        consecutive=True,
    )


def test_httpx_mock_unmatched_request_with_matched_and_unmatched_responses(
    testdir: Testdir,
) -> None:
    testdir.makepyfile(
        """
        import httpx
        import pytest

        def test_httpx_mock_unmatched_request_with_matched_and_unmatched_responses(httpx_mock):
            # Sent response
            httpx_mock.add_response(url="https://foo.tld")
            # This response will not be sent (because of a typo in the URL)
            httpx_mock.add_response(url="https://foo2.tld")
            # Sent response
            httpx_mock.add_response(url="https://foo.tld")
            # This response will not be sent (because test execution failed earlier)
            httpx_mock.add_response(url="https://foo3.tld")
            
            with httpx.Client() as client:
                client.get("https://foo.tld")
                client.get("https://foo.tld")
                # This request will not be matched
                client.get("https://foo22.tld")
                # This code will not be reached
                client.get("https://foo3.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, failed=1)
    # Assert the error that occurred
    result.stdout.fnmatch_lines(
        [
            "*httpx.TimeoutException: No response can be found for GET request on https://foo22.tld amongst:",
            "*- Match any request on https://foo2.tld",
            "*- Match any request on https://foo3.tld",
            "*- Already matched any request on https://foo.tld",
            "*- Already matched any request on https://foo.tld",
            "*",
            "*If you wanted to reuse an already matched response instead of registering it again, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-a-response-for-more-than-one-request",
        ],
        consecutive=True,
    )
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match any request on https://foo2.tld",
            "*  - Match any request on https://foo3.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )


def test_httpx_mock_unmatched_request_with_matched_and_unmatched_reusable_responses(
    testdir: Testdir,
) -> None:
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.httpx_mock(can_send_already_matched_responses=True)
        def test_httpx_mock_unmatched_request_with_matched_and_unmatched_responses(httpx_mock):
            # Sent response
            httpx_mock.add_response(url="https://foo.tld")
            # This response will not be sent (because of a typo in the URL)
            httpx_mock.add_response(url="https://foo33.tld")
            # Sent response
            httpx_mock.add_response(url="https://foo2.tld")
            # This response will not be sent (because test execution failed earlier)
            httpx_mock.add_response(url="https://foo4.tld")
            
            with httpx.Client() as client:
                client.get("https://foo.tld")
                client.get("https://foo2.tld")
                client.get("https://foo.tld")
                # This request will not be matched
                client.get("https://foo3.tld")
                # This code will not be reached
                client.get("https://foo2.tld")
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, failed=1)
    # Assert the error that occurred
    result.stdout.fnmatch_lines(
        [
            "*httpx.TimeoutException: No response can be found for GET request on https://foo3.tld amongst:",
            "*- Match every request on https://foo33.tld",
            "*- Match every request on https://foo4.tld",
            "*- Match every request on https://foo.tld",
            "*- Match every request on https://foo2.tld",
        ],
        consecutive=True,
    )
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match every request on https://foo33.tld",
            "*  - Match every request on https://foo4.tld",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )


def test_httpx_mock_should_mock_sync(testdir: Testdir) -> None:
    """
    Non mocked requests should go through while other requests should be mocked.
    """
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.httpx_mock(should_mock=lambda request: request.url.host != "localhost")
        def test_httpx_mock_should_mock_sync(httpx_mock):
            httpx_mock.add_response()
            
            with httpx.Client() as client:
                # Mocked request
                client.get("https://foo.tld")
            
                # Non mocked request
                with pytest.raises(httpx.ConnectError):
                    client.get("https://localhost:5005")
            
            # Assert that a single request was mocked
            assert len(httpx_mock.get_requests()) == 1
            
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_httpx_mock_should_mock_async(testdir: Testdir) -> None:
    """
    Non mocked requests should go through while other requests should be mocked.
    """
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.asyncio
        @pytest.mark.httpx_mock(should_mock=lambda request: request.url.host != "localhost")
        async def test_httpx_mock_should_mock_async(httpx_mock):
            httpx_mock.add_response()
            
            async with httpx.AsyncClient() as client:
                # Mocked request
                await client.get("https://foo.tld")
            
                # Non mocked request
                with pytest.raises(httpx.ConnectError):
                    await client.get("https://localhost:5005")
            
            # Assert that a single request was mocked
            assert len(httpx_mock.get_requests()) == 1
            
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_httpx_mock_options_on_multi_levels_are_aggregated(testdir: Testdir) -> None:
    """
    Test case ensures that every level provides one parameter that should be used in the end

    global (actually registered AFTER module): assert_all_responses_were_requested (tested by putting unused response)
    module: assert_all_requests_were_expected (tested by not mocking one URL)
    test: should_mock (tested by calling 3 URls, 2 mocked, the other one not)
    """
    testdir.makeconftest(
        """
        import pytest


        def pytest_collection_modifyitems(session, config, items):
            for item in items:
                item.add_marker(pytest.mark.httpx_mock(assert_all_responses_were_requested=False))
    """
    )
    testdir.makepyfile(
        """
        import httpx
        import pytest

        pytestmark = pytest.mark.httpx_mock(assert_all_requests_were_expected=False, should_mock=lambda request: request.url.host != "https://foo.tld")

        @pytest.mark.asyncio
        @pytest.mark.httpx_mock(should_mock=lambda request: request.url.host != "localhost")
        async def test_httpx_mock_options_on_multi_levels_are_aggregated(httpx_mock):
            httpx_mock.add_response(url="https://foo.tld", headers={"x-pytest-httpx": "this was mocked"})
            
            # This response will never be used, testing that assert_all_responses_were_requested is handled 
            httpx_mock.add_response(url="https://never_called.url")
            
            async with httpx.AsyncClient() as client:
                # Assert that previously set should_mock was overridden
                response = await client.get("https://foo.tld")
                assert response.headers["x-pytest-httpx"] == "this was mocked"
            
                # Assert that latest should_mock is handled
                with pytest.raises(httpx.ConnectError):
                    await client.get("https://localhost:5005")
            
                # Assert that assert_all_requests_were_expected is the one at module level
                with pytest.raises(httpx.TimeoutException):
                    await client.get("https://unexpected.url")
            
            # Assert that 2 requests out of 3 were mocked 
            assert len(httpx_mock.get_requests()) == 2
            
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_invalid_marker(testdir: Testdir) -> None:
    """
    Unknown marker keyword arguments should raise a TypeError.
    """
    testdir.makepyfile(
        """
        import pytest

        @pytest.mark.httpx_mock(foo=123)
        def test_invalid_marker(httpx_mock):
            pass
            
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1)
    result.stdout.re_match_lines([r".*got an unexpected keyword argument 'foo'"])


def test_mandatory_response_not_matched(testdir: Testdir) -> None:
    """
    is_optional MUST take precedence over assert_all_responses_were_requested.
    """
    testdir.makepyfile(
        """
        import httpx
        import pytest

        @pytest.mark.httpx_mock(assert_all_responses_were_requested=False)
        def test_mandatory_response_not_matched(httpx_mock):
            # This response is optional and the fact that it was never requested should not trigger anything
            httpx_mock.add_response(url="https://test_url")
            # This response MUST be requested
            httpx_mock.add_response(url="https://test_url2", is_optional=False)
            
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, passed=1)
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match any request on https://test_url2",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )


def test_reusable_response_not_matched(testdir: Testdir) -> None:
    testdir.makepyfile(
        """
        import httpx

        def test_reusable_response_not_matched(httpx_mock):
            httpx_mock.add_response(url="https://test_url2", is_reusable=True)
            
    """
    )
    result = testdir.runpytest()
    result.assert_outcomes(errors=1, passed=1)
    # Assert the teardown assertion failure
    result.stdout.fnmatch_lines(
        [
            "*AssertionError: The following responses are mocked but not requested:",
            "*  - Match every request on https://test_url2",
            "*  ",
            "*  If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested",
        ],
        consecutive=True,
    )