Browse Source

testing Kapitel (Vorbereitung)

develop
Raspithek 2 years ago
parent
commit
995f9ff194
  1. 2
      testing/.gitignore
  2. BIN
      testing/Fib-Testdaten.xlsx
  3. BIN
      testing/__pycache__/fib.cpython-39.pyc
  4. 13
      testing/fib.py
  5. 20
      testing/testfib.py
  6. 45
      testing/unittestfib.py

2
testing/.gitignore

@ -0,0 +1,2 @@
./__pycache__
./network/__pycache__/*

BIN
testing/Fib-Testdaten.xlsx

Binary file not shown.

BIN
testing/__pycache__/fib.cpython-39.pyc

Binary file not shown.

13
testing/fib.py

@ -0,0 +1,13 @@
# fib.py
import sys
# berechnet rekursiv die Fibonaccizahl n.
def fib(n):
# Rekursionsbedingung
if n in [0,1]:
return 1
else:
# Rekursionsaufruf
return fib(n-1) + fib(n-2)

20
testing/testfib.py

@ -0,0 +1,20 @@
from fib import fib
import logging
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
logging.debug('Test startet')
result5 = fib(5)
result8 = fib(8)
if result5 == 8 and result8 == 34:
logging.info('Tests erfolgreich')
else:
logging.error('Tests fehlerhaft')
logging.debug('Testende')

45
testing/unittestfib.py

@ -0,0 +1,45 @@
import unittest
from fib import fib
import logging
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
class TestFib(unittest.TestCase):
# Methode zur Division a/b.
def divide(self, a,b):
if b == 0:
raise ValueError('Dividend darf nicht 0 sein.')
return int(a/b)
# Die setUp() Methode wird zu Beginn jedes Testcases aufgrufen
def setUp(self):
logging.debug('setting up test')
self.results = [1,1,2,3,5,8,13,21,34,55]
#die beiden Tests aus ./testfib.py mit unittest
def test_fib(self):
# assertEqual(n,r) testet, ob das Resultat r der Vorgabe n entspricht.
self.assertEqual(8,fib(5))
self.assertEqual(34,fib(8))
def test_mult_fib(self):
results =[1,1,2,3,5,8,13,21,34,55]
for n in range(0,9):
self.assertEqual(results[n],fib(n))
def test_divide(self):
self.assertEqual(2, self.divide(2,1))
self.assertEqual(2, self.divide(4,2))
self.assertEqual(3, self.divide(6,2))
with self.assertRaises(ValueError):
self.divide(1,0)
if __name__ == '__main__':
unittest.main()
Loading…
Cancel
Save