Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, March 19, 2021

Nose Test using Python

 Nose test is one of the unit testing tool in python to run the unit tests.


Below code show who to run the test for circle with various test case. 

# Import the Circle class from the circle module using the expression from proj.circle import Circle
# Import assert_raises from nose.tools using the expression from nose.tools import assert_raises, eq_.
# Use eq_ method for assert, and to check. Don't use assert statement.
from proj.circle import Circle
from nose.tools import assert_raises, eq_


# Define a nose test class 'TestingCircleCreation'
class TestingCircleCreation:
# Define a nose test method 'test_creating_circle_with_numeric_radius', which creates a circle with radius 2.5, and check if its radius value is 2.5 using eq_ method.
def test_creating_circle_with_numeric_radius(self):
c1 = Circle(2.5)
r = c1.radius
eq_(r,2.5)


# Define a nose test method 'test_creating_circle_with_negative_radius', which checks if the ValueError exception is raised with the error message "radius must be between 0 and 1000 inclusive" using eq_ method, while creating a circle of radius -2.5.
# Hint: Use assert_raises and with.
def test_creating_circle_with_negative_radius(self):
#eq_()
with assert_raises(ValueError) as e:
c = Circle(-2.5)
eq_(str(e.exception), "radius must be between 0 and 1000 inclusive")


# Define a nose test method 'test_creating_circle_with_greaterthan_radius', which checks if the ValueError exception is raised with the error message "radius must be between 0 and 1000 inclusive" using eq_ method, while creating circle of radius 1000.1 .
# Hint: Use assert_raises and with
def test_creating_circle_with_greaterthan_radius(self):
#eq_()
with assert_raises(ValueError) as e:
c = Circle(1000.1)
eq_(str(e.exception), "radius must be between 0 and 1000 inclusive")


# Define a nose test method 'test_creating_circle_with_nonnumeric_radius', which checks if the TypeError exception is raised with the error message "radius must be a number" using eq_ method, while creating circle of radius 'hello' .
# Hint: Use assert_raises and with.
def test_creating_circle_with_nonnumeric_radius(self):
with assert_raises(TypeError) as e:
c=Circle("hello")
eq_(str(e.exception), "radius must be a number")






#Define a nose test class 'TestCircleArea'
class TestCircleArea:
# Define a nose test method 'test_circlearea_with_random_numeric_radius', which creates a circle 'c1' with radius 2.5, and check if its computed area is 19.63 using eq_ method.
def test_circlearea_with_random_numeric_radius(self):
c1=Circle(2.5)
eq_(c1.area(), 19.63)


# Define a nose test method 'test_circlearea_with_min_radius', which creates a circle 'c2' with radius 0, and check if its computed area is 0 using eq_ method.
def test_circlearea_with_min_radius(self):
c2=Circle(0)
eq_(int(c2.area()), 0)


# Define a nose test method 'test_circlearea_with_max_radius', which creates a circle 'c3' with radius 1000, and check if its computed area is 3141592.65 using eq_ method.
def test_circlearea_with_max_radius(self):
c3=Circle(1000)
eq_(c3.area(), 3141592.65)


# Define a nose test class 'TestCircleCircumference'
class TestCircleCircumference:
# Define a nose test method 'test_circlecircum_with_random_numeric_radius', which creates a circle 'c1' with radius 2.5, and check if its computed circumference is 15.71 using eq_ method.
def test_circlecircum_with_random_numeric_radius(self):
c1=Circle(2.5)
eq_(c1.circumference(), 15.71)


# Define a nose test method 'test_circlecircum_with_min_radius', which creates a circle 'c2' with radius 0, and check if its computed circumference is 0 using eq_ method.
def test_circlecircum_with_min_radius(self):
c2=Circle(0)
eq_(int(c2.circumference()), 0)


# Define a nose test method 'test_circlecircum_with_max_radius', which creates a circle 'c3' with radius 1000, and check if its computed circumference is 6283.19 using eq_ method.
def test_circlecircum_with_max_radius(self):
c3=Circle(1000)
eq_(c3.circumference(), 6283.19)







Tuesday, January 5, 2021

Create test script in python3 using unittest

Below sample shows how to create unit test script using unittest in python for testing a Circle,area and circumference


Monday, January 4, 2021

Create test script using doctest test in python3

Sample script for creating a doc test for Circle, radius,area and circumference of a Circle. 

This one check the polindrome of a given number using doctest

 
This one check the Circle method

Sunday, January 3, 2021

Sunday, December 9, 2018

SET DICT LIST TUPLE SLICING in Python

set
---------

a = set(('10','20','30','40'))
b = set(('30','60'))
u=a.union(b)
i=a.intersection(b)
d=a.difference(b)
sd=a.symmetric_difference(b)
print('u = ',u)
print('i = ',i)
print('d = ',d)
print('sd = ',sd)

dict
---------

d1 = dict()
print(d1)
d2 = dict(p='play',t='talk')
print(d2)
d2['v'] = 'vibe'
d2['d'] = 'docs'
print(d2)
del(d2['v'])
print(d2)

list
-------

emplist1 = list()
print(emplist1)
emplist1.append([9,10])
print(emplist1)
emplist2 = []
print (emplist2)
emplist2 = ['a', 'b', 'c']
print (emplist2)
e=emplist2.pop()
print (emplist2)
print(e)



Tuple
----------

tup1=tuple()
print(tup1)
tup2=tuple('Welcome')
print(tup2)
print('e' in tup2)
print(tup2.count('e'))
print(tup2.index('e'))
print(len(tup2))

Slicing
-------------

k = [3, 4, 5, 6, 7, 8, 9]
print(k[::2])
print(k[0:4])
print(k[1:10:2])

Generate Positive Infinity and negative Values using Python

We can generate the Infinity values using Python. We need import math library to do this.


import math
x=math.inf

y=-math.inf

you need to keep - before to generate negative infinity

print(x)

The above prints positive infinity

print(y)

Above one print negative infinity

print(math.isinf(x))

prints true

print(math.isinf(y))

print true