0

There's only a single line of code, and I won't bother you with the data as it is quite large.

I have a list of all requirements for some software in allRequirements and a second list of those requirements for which a test case exists in requirementWithCoverage.

I want to generate a list of those requirements with no test case coverage.

Lots of websites and S.O questions give the answer as this :

notCovered = list(set(allRequirements) - set(requirementWithCoverage))

However:

len(set(allRequirements)) is 779 and
len(set(requirementWithCoverage)) is 201, BUT
len(set(notCovered)) is 650

Obviously, I am making a very basic mistake - but for the life of me I can't see it. What am I doing wrongly?

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 1
    Can you try symmetric_difference as in the answer of this post: https://stackoverflow.com/questions/3462143/get-difference-between-two-lists – Nico Müller Aug 20 '18 at 14:06
  • 1
    Could you please confirm, that sets of the lists `requirementsWithCoverage` and `notCovered` are subsets of `allRequirements` by using `set(requirementsWithCoverage).issubset(set(allRequirements))` and `set(notCovered).issubset(set(allRequirements))`. My guess is there is something wrong with the coverage list. – Igl3 Aug 20 '18 at 14:15
  • the latter is True, but the former is False. That should be enough for me to debug it. If you can phrase that as an answer, please do – Mawg says reinstate Monica Aug 20 '18 at 14:18

1 Answers1

3

What you observe is due to the fact that requirementWithCoverage contains elements that are not in allRequirements. Here is an example:

allRequirements         = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
requirementWithCoverage = [1,                7,           11, 12] # 11 and 12 are unexpectedly there
notCovered              = list(set(allRequirements) - set(requirementWithCoverage))

print(len(allRequirements))          # 10
print(len(requirementWithCoverage))  #  4
print(len(notCovered))               #  8 (6 was expected)

You can confirm this by printing the returned value of set(requirementWithCoverage).issubset(set(allRequirements)) which should be False, whereas you expected it to be True.
And even better, you can print the unexpected elements of requirementWithCoverage through:

print(set(requirementWithCoverage) - set(allRequirements))
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Laurent H.
  • 6,316
  • 1
  • 18
  • 40