0

so I would like to test a code snippet that I know would work:

def find_primes(n):
    primeList = []
    for x in range(2, n+1):
        for y in range(2, x):
            if x % y == 0:
                break
        else:
            primeList.append(x)
    print(primeList)

This is to find prime number list in a specific rage from 2 to whatever I want. In this case, if I try find_primes(10), I will get [2, 3, 5, 7]

But when I try the unittest:

import unittest
class PNumTest(unittest.TestCase):

    def test_find_prime_number(self):
        self.assertEqual(find_primes(10), [2, 3, 5, 7])

unittest.main()

it shows:

[2, 3, 5, 7]
[2, 3, 5, 7]
F
======================================================================
FAIL: test_find_prime_number (__main__.PnumTEST)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "main.py", line 17, in test_find_prime_number
    self.assertEqual(find_primes(10), [2, 3, 5, 7])
AssertionError: None != [2, 3, 5, 7]

----------------------------------------------------------------------
Ran 1 test in 0.002s

I am trying to understand what went wrong.

jxie0755
  • 1,682
  • 1
  • 16
  • 35

0 Answers0