#!/usr/bin/env python
import operator
def factorial (n):
if n < 0 :
raise ValueError ( "Factorial can't be calculated for negative numbers." )
if type (n) is float or type (n) is complex :
raise TypeError ( "Factorial doesn't use Gamma function." )
if n == 0 :
return 1
return reduce (operator . mul, range ( 1 , n + 1 ))
if __name__ == '__main__' :
n = input ( 'Enter the positive number: ' )
print '{0}! = {1}' . format(n, factorial( int (n)))
import unittest
from main import factorial
class TestFactorial (unittest . TestCase):
def test_calculation ( self ):
self . assertEqual( 720 , factorial( 6 ))
def test_negative ( self ):
self . assertRaises( ValueError , factorial, -1 )
def test_float ( self ):
self . assertRaises( TypeError , factorial, 1.25 )
def test_zero ( self ):
self . assertEqual( 1 , factorial( 0 ))
$ nosetests --with-coverage --cover-erase
....
Name Stmts Exec Cover Missing
-------------------------------------
main 12 10 83% 16-17
----------------------------------------------------------------------
Ran 4 tests in 0.021s
OK
class TestMain (unittest . TestCase):
class FakeStream :
def __init__ ( self ):
self . msgs = []
def write ( self , msg):
self . msgs . append(msg)
def readline ( self ):
return '5'
def test_use_case ( self ):
fake_stream = self . FakeStream()
try :
sys . stdin = sys . stdout = fake_stream
execfile ( 'main.py' , { '__name__' : '__main__' })
self . assertEqual( '5! = 120' , fake_stream . msgs[ 1 ])
finally :
sys . stdin = sys . __stdin__
sys . stdout = sys . __stdout__
$ nosetests --with-coverage --cover-erase
.....
Name Stmts Exec Cover Missing
-------------------------------------
main 12 12 100%
----------------------------------------------------------------------
Ran 5 tests in 0.032s
OK
$ python3 main.py
File "main.py", line 17
print '{0}! = {1}'.format(n, factorial(int(n)))
^
SyntaxError: invalid syntax
#!/usr/bin/env python
import operator
def factorial (n):
if n < 0 :
raise ValueError ( "Factorial can't be calculated for negative numbers." )
if type (n) is float or type (n) is complex :
raise TypeError ( "Factorial doesn't use Gamma function." )
if n == 0 :
return 1
return reduce (operator . mul, range ( 1 , n + 1 ))
if __name__ == '__main__' :
n = input ( 'Enter the positive number: ' )
print ( '{0}! = {1}' . format(n, factorial( int (n))))
$ python3 main.py
Enter the positive number: 0
0! = 1
$ nosetests3
E...E
======================================================================
ERROR: test_calculation (tests.TestFactorial)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/nuald/workspace/factorial/tests.py", line 9, in test_calculation
self.assertEqual(720, factorial(6))
File "/home/nuald/workspace/factorial/main.py", line 12, in factorial
return reduce(operator.mul, range(1, n + 1))
NameError: global name 'reduce' is not defined
======================================================================
ERROR: test_use_case (tests.TestMain)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/nuald/workspace/factorial/tests.py", line 38, in test_use_case
execfile('main.py', {'__name__': '__main__'})
NameError: global name 'execfile' is not defined
----------------------------------------------------------------------
Ran 5 tests in 0.010s
FAILED (errors=2)
#!/usr/bin/env python
import operator
from functools import reduce
def factorial (n):
if n < 0 :
raise ValueError ( "Factorial can't be calculated for negative numbers." )
if type (n) is float or type (n) is complex :
raise TypeError ( "Factorial doesn't use Gamma function." )
if n == 0 :
return 1
return reduce (operator . mul, range ( 1 , n + 1 ))
if __name__ == '__main__' :
n = input ( 'Enter the positive number: ' )
print ( '{0}! = {1}' . format(n, factorial( int (n))))
import sys
import unittest
from main import factorial
class TestFactorial (unittest . TestCase):
def test_calculation ( self ):
self . assertEqual( 720 , factorial( 6 ))
def test_negative ( self ):
self . assertRaises( ValueError , factorial, -1 )
def test_float ( self ):
self . assertRaises( TypeError , factorial, 1.25 )
def test_zero ( self ):
self . assertEqual( 1 , factorial( 0 ))
class TestMain (unittest . TestCase):
class FakeStream :
def __init__ ( self ):
self . msgs = []
def write ( self , msg):
self . msgs . append(msg)
def readline ( self ):
return '5'
def test_use_case ( self ):
fake_stream = self . FakeStream()
try :
sys . stdin = sys . stdout = fake_stream
obj_code = compile ( open ( 'main.py' ) . read(), 'main.py' , 'exec' )
exec (obj_code, { '__name__' : '__main__' })
self . assertEqual( '5! = 120' , fake_stream . msgs[ 1 ])
finally :
sys . stdin = sys . __stdin__
sys . stdout = sys . __stdout__
$ nosetests --with-coverage --cover-erase
.....
Name Stmts Exec Cover Missing
-------------------------------------
main 13 13 100%
----------------------------------------------------------------------
Ran 5 tests in 0.038s
OK
$ nosetests3 --with-coverage --cover-erase
.....
Name Stmts Miss Cover Missing
-------------------------------------
main 13 0 100%
----------------------------------------------------------------------
Ran 5 tests in 0.018s
OK
Source: https://habr.com/ru/post/97075/
All Articles