
from http_crawler import crawl


def test_no_links_are_broken(baseurl):
    """
    Check that all internal links point to working pages.
    """
    responses = []
    for rsp in crawl(baseurl, follow_external_links=False):
        responses.append(rsp)

    failed_responses = [rsp for rsp in responses if rsp.status_code != 200]
    failures = [
        '%s (%d)' % (rsp.url, rsp.status_code)
        for rsp in failed_responses
    ]
    print('\n'.join(failures))
    assert len(failures) == 0


from http_crawler import crawl
import pytest

_responses = []


@pytest.fixture
def responses(baseurl):
    if not _responses:
        for rsp in crawl(baseurl, follow_external_links=False):
            _responses.append(rsp)
    return _responses


def test_no_links_are_broken(responses):
    """
    Check that all internal links point to working pages.
    """
    failed_responses = [rsp for rsp in responses if rsp.status_code != 200]
    failures = [
        '%s (%d)' % (rsp.url, rsp.status_code)
        for rsp in failed_responses
    ]
    print('\n'.join(failures))
    assert len(failures) == 0

Move the responses to a cacheable fixture
Move the responses to a cacheable fixture

  
  from http_crawler import crawl
+ import pytest
+ 
+ _responses = []
  
  
+ @pytest.fixture
+ def responses(baseurl):
+     if not _responses:
+         for rsp in crawl(baseurl, follow_external_links=False):
+             _responses.append(rsp)
+     return _responses
+ 
+ 
- def test_no_links_are_broken(baseurl):
+ def test_no_links_are_broken(responses):
      """
      Check that all internal links point to working pages.
      """
-     responses = []
-     for rsp in crawl(baseurl, follow_external_links=False):
-         responses.append(rsp)
- 
      failed_responses = [rsp for rsp in responses if rsp.status_code != 200]
      failures = [
          '%s (%d)' % (rsp.url, rsp.status_code)
          for rsp in failed_responses
      ]
      print('\n'.join(failures))
      assert len(failures) == 0
  
Move the responses to a cacheable fixture
## Code Before:

from http_crawler import crawl


def test_no_links_are_broken(baseurl):
    """
    Check that all internal links point to working pages.
    """
    responses = []
    for rsp in crawl(baseurl, follow_external_links=False):
        responses.append(rsp)

    failed_responses = [rsp for rsp in responses if rsp.status_code != 200]
    failures = [
        '%s (%d)' % (rsp.url, rsp.status_code)
        for rsp in failed_responses
    ]
    print('\n'.join(failures))
    assert len(failures) == 0

## Instruction:
Move the responses to a cacheable fixture
## Code After:

from http_crawler import crawl
import pytest

_responses = []


@pytest.fixture
def responses(baseurl):
    if not _responses:
        for rsp in crawl(baseurl, follow_external_links=False):
            _responses.append(rsp)
    return _responses


def test_no_links_are_broken(responses):
    """
    Check that all internal links point to working pages.
    """
    failed_responses = [rsp for rsp in responses if rsp.status_code != 200]
    failures = [
        '%s (%d)' % (rsp.url, rsp.status_code)
        for rsp in failed_responses
    ]
    print('\n'.join(failures))
    assert len(failures) == 0
