eloidieme / DD2480_Coverage

Scrapy, a fast high-level web crawling & scraping framework for Python.
https://scrapy.org
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Improve the test coverage of the _link_allowed function #8

Open lovisastrange opened 8 months ago

lovisastrange commented 8 months ago

Keep a record (copy) of your coverage before you start working on new tests. Furthermore, make sure you add the new tests on a different branch than the one you used for coverage instrumentation.

Having identified “weak spots” in coverage, try to improve coverage with additional test cases.

  1. Identify the requirements that are tested or untested by the given test suite.

  2. Document the requirements (as comments), and use them later as assertions.

  3. Create new test cases as needed to improve branch coverage in the given functions. Can you call the function directly? Can you expand on existing tests Do you have to add additional interfaces to the system (as public methods) to make it possible to set up test data structures?

  4. If you have 100 % branch coverage, you can choose other functions or think about path coverage. You may cover all branches in a function, but what does this mean for the combination of branches? Consider the existing tests by hand and check how they cover the branches (in which combinations).

lovisastrange commented 8 months ago

Coverage before new tests for both _link_allowed function and methods in link class:

lovisastrange commented 8 months ago

Requirements identified:

For _link_allowed, the requirements that are found are

For the link class, we instead have in the init and eq functions that

lovisastrange commented 8 months ago

The untested requirements is Req 1 for _link_allowed and 1 and 2 for the link class

lovisastrange commented 8 months ago

The test on _link_allowed test that the extracted links is an empty list when given a non-valid link. The test test_not_valid_link is created in the file scrapy/tests/test_linkextractors.py. We give the link test.test, and check that

 self.assertEqual(lx.extract_links(response),[])

The coverage after adding this test is

linkAllowedAfter

lovisastrange commented 8 months ago

Now, we look at the test added for the link class.

The tests were added in scrapy/tests/test_link.py and cover one of the un-tested cases each. In the first one, test_if_string, we check that the function raises a TypeError when an integer is given instead of a string.

Another test, test_equals_not_link checks that we get a NotImplementedError if we try to compare a link object with a non-link object, more specifically an integer.

The coverage after adding the test is

linkCoverageAfter