def test_odd_or_even(self): for num in np.arange(0, 12, 2): test = sm(num) assert test.odd_or_even() == 'Even' for num in np.arange(1, 11, 2): test = sm(num) assert test.odd_or_even() == 'Odd'
def test_factorial(self): test = sm(-4) with raises(ValueError) as exception: test.factorial() for num in range(5): test = sm(num) assert test.factorial() == math.factorial(num)
def test_square_root(self): test = sm(-4) with raises(ValueError) as exception: test.square_root() for num in np.arange(5): test = sm(num) assert test.square_root() == np.sqrt(num)
def test_power(self): test = sm(3) with raises(ValueError) as exception: test.power(False) for num in range(5): test = sm(num) for x in np.arange(4): assert test.power(x) == math.pow(num, x)
def test_odd_or_even(self): """ Check that 0 returns as Even and other positive cases """ zero = sm(0) assert zero.odd_or_even() == 'Even' one = sm(1) assert one.odd_or_even() == 'Odd' two = sm(2) assert two.odd_or_even() == 'Even' minusone = sm(-1) assert minusone.odd_or_even() == 'Odd'
def test_square_root(self): """ Positive tests for 0, int result, float result and complex result """ zero = sm(0) assert zero.square_root() == 0.0 four = sm(4) assert four.square_root() == 2 twelve = sm(12) assert twelve.square_root() == 3.4641016151377544 minusfour = sm(-4) assert minusfour.square_root() == (1.2246467991473532e-16+2j) assert type(minusfour.square_root()) == complex
def test_constructor_1(self): with raises(TypeError): sm(3.2) with raises(TypeError): sm(-123.2) with raises(TypeError): sm(3 / 2.5)
def test_factorial_0(self): zero = sm(0) assert zero._factorial(0) == 1
def test_square(self): twelve = sm(12) assert sm.square(twelve) == 12**2
def test_constructor_type(self): """ Check that the constructor creates an 'sm'-type object """ twelve = sm(12) assert type(twelve) == sm
def test_power_2(self): test_num_neg = sm(0 - test_num.number) assert test_num_neg.power(2) == test_num.power(rand_index)
def test_factorial(self): a = sm(5) assert a.factorial() == 120
def test_power_inputs(self): """ Check result for float input to power """ twelve = sm(12) assert twelve.power(3.0) == 12**3.0
def test_power_default(self): """ Check that the power defaults to 3 """ twelve = sm(12) assert twelve.power() == 12**3
from pytest import raises import random import math from simplemaths.simplemaths import SimpleMaths as sm rand_int = random.randint(1, 12) test_num = sm(rand_int) class TestSimpleMaths(): def test_constructor_pos(self): # positive constructor test for int type assert type(test_num.number) == int def test_constructor_float(self): # negative constructor test for float input rand_float = random.uniform(1, 12) with raises(TypeError): a = sm(rand_float) def test_constructor_string(self): # positive constructor test for int type rand_string = str(test_num) with raises (TypeError): b = sm(rand_string) def test_square(self): assert test_num.square() == test_num.number ** 2 def test_factorial(self): test_fac = math.factorial(test_num.number)
def test_constructor_float(self): # negative constructor test for float input rand_float = random.uniform(1, 12) with raises(TypeError): a = sm(rand_float)
def test_constructor_string(self): # positive constructor test for int type rand_string = str(test_num) with raises (TypeError): b = sm(rand_string)
def test_square(self): for num in range(5): test = sm(num) assert test.square() == np.square(num)
def test_constructor_input_boolean(self): with raises(ValueError) as exception: test = sm(False)
def test_constructor_input_complex(self): with raises(ValueError) as exception: test = sm(2j)
def test_constructor_input_string(self): with raises(ValueError) as exception: test = sm('a')
def test_factorial_proper(self): twelve = sm(12) assert sm.factorial(twelve) == 479001600 zero = sm(0) assert sm.factorial(zero) == 1
def test_power(self): """ Check result for int inputs to power """ twelve = sm(12) assert twelve.power(0) == 1 minustwo = sm(-2) assert minustwo.power(2) == 4
def test_non_int_power(self): a = sm(2) assert a.power(2.5) == pytest.approx(5.656, 0.001)
def test_constructor_input(self): """ Check that some non-integer inputs will fail """ with raises(TypeError) as exception: sm(1.0) with raises(TypeError) as exception: sm("string")
def test_constructor_input_float(self): with raises(ValueError) as exception: test = sm(0.1)
def test_power_inputs(self): """ Check that a non-int/float input will fail """ twelve = sm(12) with raises(TypeError) as exception: twelve.power('three')
def test_constructor_input_empty(self): """ Check that an empty input will fail """ with raises(TypeError) as exception: sm()
def test_constructor_number(self): """ Check that the .number attribute returns the inputted integer """ twelve = sm(12) assert twelve.number == 12
def test_factorial_neg(self): test_num_neg = sm(0 - test_num.number) with raises(Exception): c = test_num_neg.factorial()