RITIK KUMAR
RITIK KUMAR

Reputation: 137

coverage report showing zero

this code is saved with calc.py. This is a basic calculator program in which I have used flask module.

def sum(number1,number2):
    result= number1+number2
    return result

def sub(number1,number2):
    result= number1-number2
    return result
def mul(number1,number2):
    result= number1*number2
    return result

def div(number1,number2):
    result= number1/number2
    return result

This program is saved with testing.py name on my machine. the module used in this is unitest and calc from the above. I am doing testing here. since I am getting a 0% coverage report. please help me out. please comment on the links if possible.

import unittest
import calc

class TestCalc(unittest.TestCase):

    def test_sum1(self): # function to call self
#testing is being done for addition
        result=calc.sum(10,15) #calculation module is being called
        self.assertEqual(result,25)
        print('test 1 of sum successful')

    def test_sum2(self):# function to call self
        #testing is being done for addition
        result=calc.sum(12,7)#calculation module is being called
        self.assertEqual(result,19)
        print('test 2 of sum successful')

    def test_sum3(self):       # function to call self
        #testing is being done for addition
        result=calc.sum(15,5)#calculation module is being called
        self.assertEqual(result,20)
        print('test 3 of sum successful')

    def test_sum4(self):# function to call self
        #testing is being done for addition
        result=calc.sum(12,8)#calculation module is being called
        self.assertEqual(result,20)
        print('test 4 of sum successful')

    def test_sum5(self):# function to call self
        #testing is being done for addition
        result=calc.sum(10,15)#calculation module is being called
        self.assertEqual(result,25)
        print('test 5 of sum successful')

    def test_sum6(self):# function to call self
        #testing is being done for addition
        result=calc.sum(10,15)#calculation module is being called
        self.assertEqual(result,25)
        print('test 6 of sum successful')

    def test_sum7(self):# function to call self
        #testing is being done for addition
        result=calc.sum(10,15)#calculation module is being called
        self.assertEqual(result,25)
        print('test 7 of sum successful')

    def test_sum8(self):# function to call self
        #testing is being done for addition
        result=calc.sum(12,7)#calculation module is being called
        self.assertEqual(result,19)
        print('test 8 of sum successful')

    def test_sum9(self):# function to call self
        #testing is being done for addition
        result=calc.sum(10,15)#calculation module is being called
        self.assertEqual(result,25)
        print('test 9  of sum successful')

    def test_sum10(self):# function to call self
        #testing is being done for addition
        result=calc.sum(12,7)#calculation module is being called
        self.assertEqual(result,19)
        print('test 10 of sum successful')

    def test_sub1(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(10,5)#calculation module is being called
        self.assertEqual(result,5)
        print('test 1 of sub successful')

    def test_sub2(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(12,7)#calculation module is being called
        self.assertEqual(result,5)
        print('test 2 of sub successful')

    def test_sub3(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(15,5)#calculation module is being called
        self.assertEqual(result,10)
        print('test 3 of sub successful')

    def test_sub4(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(12,8)#calculation module is being called
        self.assertEqual(result,4)
        print('test 4 of sub successful')

    def test_sub5(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(10,15)#calculation module is being called
        self.assertEqual(result,-5)
        print('test 5 of sub successful')

    def test_sub6(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(100,25)#calculation module is being called
        self.assertEqual(result,75)
        print('test 6 of sub successful')

    def test_sub7(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(15,5)#calculation module is being called
        self.assertEqual(result,10)
        print('test 7 of sub successful')

    def test_sub8(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(12,8)#calculation module is being called
        self.assertEqual(result,4)
        print('test 8 of sub successful')

    def test_sub9(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(10,15)#calculation module is being called
        self.assertEqual(result,-5)
        print('test 9 of sub successful')

    def test_sub10(self):# function to call self
        #testing is being done for substraction
        result=calc.sub(100,25)#calculation module is being called
        self.assertEqual(result,75)
        print('test 10 of sub successful')

    def test_mul1(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(2,4)#calculation module is being called
        self.assertEqual(result,8)
        print('test 1 of multiplication successful')

    def test_mul2(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(6,2)#calculation module is being called
        self.assertEqual(result,12)
        print('test 2 of multiplication successful')

    def test_mul3(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(15,5)#calculation module is being called
        self.assertEqual(result,75)
        print('test 3 of multiplication successful')

    def test_mul4(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(12,8)#calculation module is being called
        self.assertEqual(result,96)
        print('test 4 of multiplication successful')

    def test_mul5(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(10,15)#calculation module is being called
        self.assertEqual(result,150)
        print('test 5 of multiplication successful')

    def test_mul6(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(5,5)#calculation module is being called
        self.assertEqual(result,25)
        print('test 6 of multiplication successful')

    def test_mul7(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(15,5)#calculation module is being called
        self.assertEqual(result,75)
        print('test 7 of multiplication successful')

    def test_mul8(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(12,8)#calculation module is being called
        self.assertEqual(result,96)
        print('test 8 of multiplication successful')

    def test_mul9(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(10,15)#calculation module is being called
        self.assertEqual(result,150)
        print('test 9 of multiplication successful')

    def test_mul10(self):# function to call self
        #testing is being done for multiplication
        result=calc.mul(5,5)#calculation module is being called
        self.assertEqual(result,25)
        print('test 10 of multiplication successful')

    def test_div1(self):# function to call self
        #testing is being done for division
        result=calc.div(10,5)#calculation module is being called
        self.assertEqual(result,2)
        print('test 1 of division successful')

    def test_div2(self):# function to call self
        #testing is being done for division
        result=calc.div(12,6)#calculation module is being called
        self.assertEqual(result,2)
        print('test 2 of division successful')

    def test_div3(self):# function to call self
        #testing is being done for division
        result=calc.div(15,5)#calculation module is being called
        self.assertEqual(result,3)
        print('test 3 of division successful')

    def test_div4(self):# function to call self
        #testing is being done for division
        result=calc.div(12,3)#calculation module is being called
        self.assertEqual(result,4)
        print('test 4 of division successful')

    def test_div5(self):# function to call self
        #testing is being done for division
        result=calc.div(14,7)#calculation module is being called
        self.assertEqual(result,2)
        print('test 5 of division successful')

    def test_div6(self):# function to call self
        #testing is being done for division
        result=calc.div(100,25)#calculation module is being called
        self.assertEqual(result,4)
        print('test 6 of division successful')

    def test_div7(self):# function to call self
        #testing is being done for division
        result=calc.div(15,5)#calculation module is being called
        self.assertEqual(result,3)
        print('test 7 of division successful')

    def test_div8(self):# function to call self
        #testing is being done for division
        result=calc.div(12,3)#calculation module is being called
        self.assertEqual(result,4)
        print('test 8 of division successful')

    def test_div9(self):# function to call self
        #testing is being done for division
        result=calc.div(14,7)#calculation module is being called
        self.assertEqual(result,2)
        print('test 9 of division successful')

    def test_div10(self):# function to call self
        #testing is being done for division
        result=calc.div(100,25)#calculation module is being called
        self.assertEqual(result,4)
        print('test 10 of division successful')

if __name__=='__main__':
    unittest.main()

I am getting a 0% coverage report, what to do??

Upvotes: 0

Views: 1826

Answers (2)

iamhimanshu0
iamhimanshu0

Reputation: 389

In order to run the coverage using unittest simply run coverage run -m unittest discover

and there is one unnecessary import you're importing

from flask import Flask,request,app
from flask import Flask,request

so change it to

from flask import Flask,request, app

Upvotes: 3

YamanSinghal
YamanSinghal

Reputation: 132

The problem might be because of coming straight to the coverage report -m testing.py command. Before this, try using command coverage run testing.py . This will run your testing file with coverage module.

Also i would recommend to use name testing_calc.py instead of only testing.py. This would help in easy accessibility of files in case of large project.

In addition, use different testing functions only when you want to test different functions. Like here you are using functions sum1,sum2,...,sum10 to test only one function sum. Try incorporating all the test cases for one function into one testing function. This will make your code free from redundant lines of code.

Hope I might be of some help :)

Upvotes: 1

Related Questions