Example code - Unit test

From 22113
Revision as of 15:05, 6 March 2024 by WikiSysop (talk | contribs) (Created page with "I decided to make some unit tests for my prime number generator, I used as an example last week. I make two files - one file containing the class and one file containing the tests. The files are supposed to be in the same folder. Notice I changed the name of the class a bit. <p> '''File:''' primegenerator.py <pre> # Prime number generator class primegenerator: # Class varible, known primes in consecutive order, can be extended, but must contain these knownprimes...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

I decided to make some unit tests for my prime number generator, I used as an example last week. I make two files - one file containing the class and one file containing the tests. The files are supposed to be in the same folder. Notice I changed the name of the class a bit.

File: primegenerator.py

# Prime number generator
class primegenerator:
    # Class varible, known primes in consecutive order, can be extended, but must contain these
    knownprimes = [2, 3]
    # Highest tested number for prime
    highesttested = 3

    # Instatiation
    def __init__(self, number=None):
        if number is not None:
            # Don't catch an exception below - it does want we want.
            number = int(number)
        self.target = number                
    
    # Initializing iteration
    def __iter__(self):
        if self.target is None:
            raise ValueError("No number specified")
        self.pos = 0
        return self
    
    # Find next prime
    def __next__(self):
        # Can we use the list of known primes to find the next?
        if self.pos < len(self.knownprimes):
            nextprime = self.knownprimes[self.pos]
            if nextprime >= self.target:
                raise StopIteration
            self.pos += 1
            return nextprime
        # No, start computing the next prime
        while self.target > primegenerator.highesttested+1:
            primegenerator.highesttested += 1
            if self._isprime(primegenerator.highesttested):
                self.knownprimes.append(primegenerator.highesttested)
                self.pos += 1
                return self.highesttested
        raise StopIteration

    # Private method for identifying a prime
    def _isprime(self, number):
        factor = 0
        pos = 0
        while factor*factor <= number:
            # find next potential factor either in known primes or odd numbers above last known prime
            if pos < len(self.knownprimes):
                factor = self.knownprimes[pos]
                pos += 1
            else:
                factor += 2
            # test if it truly is a factor
            if number % factor == 0:
                return False
        return True

    # It is nice be able to ask if an number is a prime
    def isprime(self, number=None):
        if number is None:
            number = self.target
        if number is None:
            raise ValueError("Number expected")
        if number in primegenerator.knownprimes:
            return True
        if number <= primegenerator.highesttested:
            return False
        return self._isprime(number)

This is my unit test file.
File: test_primegenerator.py

import pytest
from primegenerator import primegenerator