python: unittest

 

pip3 install parameterized
pip3 install pytest
pip3 install pytest-html
pip3 install coverage
pip3 install html-testRunner
 
import unittest
import unittest.mock
import unittest.loader
import unittest.case
from unittest.mock import MagicMock
import sys
import webbrowser

class SomeWork(object):
    """
    """
    def initenv(self):
        """

        :return:
        """
        print("初始化环境")

    def clearenv(self):
        """

        :return:
        """
        print("清理环境配置")
        
    @staticmethod
    def workPad():
        """"""
        print("工作中")
        


class Test(unittest.TestCase):
    """

    """
    somework = None
    someWork = None 

    @classmethod
    def setUpClass(cls):
        """

        :return:
        """
        cls.somework = SomeWork()
        cls.somework.initenv()
        cls.someWork=SomeWork

    def testcase(self):
        """

        :return:
        """
        self.somework.initenv()
        print("this is case")

    def testcase2(self):
        """

        :return:
        """
        self.someWork.workPad()
        #SomeWork.workPad()
        print("this is case2")

    @classmethod
    def tearDownClass(cls):
        """
        
        :return:
        """
        #cls.somework()
        cls.someWork
        

    def main(self, out=sys.stderr, verbosity=2):
        """
        
        :param out: 
        :param verbosity: 
        :return: 
        """
        loader = unittest.TestLoader()

        suite = loader.loadTestsFromModule(sys.modules[__name__])
        unittest.TextTestRunner(out, verbosity=verbosity).run(suite)


if __name__ == '__main__':
    
    
        # 写入报告
    with open('testclass.txt', 'w') as f:
            t=Test()
            t.main(f)

    #os.system(r'notepad testclass.txt')
    webbrowser.open('testclass.txt',new=0,autoraise=True)  #打开文档
    
    unittest.main()

  

 

# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# Datetime  : 2024/10/12 6:29
# User      : geovindu
# Product   : PyCharm
# Project   : TechnologyGame
# File      : cal.py
# explain   : 学习

__metaclass_=type

class calculator(object):
    """

    """
    def __init__(self,a:int,b:int):
        self._numa=a
        self._numb=b

    @property
    def numa(self):
        """

        :return:
        """
        return self._numa

    @numa.setter
    def numa(self,a:int):
        """

        :param a:
        :return:
        """
        self._numa=a

    @property
    def numb(self):
        """

        :return:
        """
        return self._numb

    @numb.setter
    def numb(self,b:int):
        """

        :param b:
        :return:
        """
        self._numb=b

    def Add(self):
        """

        :return:
        """
        return self._numa+self._numb

    def Subs(self):
        """

        :return:

        """
        return self._numa-self._numb

    def Multiply(self):
        """

        :return:

        """
        return self._numa*self._numb

    def Divide(self):
        """

        :return:
        """
        try:
            return self._numa/self._numb

        except ZeroDivisionError:
            print('zero division errror')
            return 0

  

 

# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:
# 描述: pip3 install parameterized   pip3 install pytest  pip3 install pytest-html  pip3 install coverage pip3 install html-testRunner
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# Datetime  : 2024/10/12 6:29
# User      : geovindu
# Product   : PyCharm
# Project   : TechnologyGame
# File      : testcal.py
# explain   : 学习

import unittest
import unittest.mock
import unittest.loader
import unittest.case
from unittest.mock import MagicMock
import sys
import os
from cal import calculator
import webbrowser
import HTMLTestRunner
import asyncio


class calulatorUnittest(unittest.TestCase):
    """
    单元测麻
    """

    def setUp(self) -> None:
        """

        :return: 
        """
        print('Test Start')

    def testBase(self):
        """

        :return: 
        """
        j = calculator(4, 2)
        self.assertEquals(j.Add(),6)
        self.assertEquals(j.Subs(),2)
        self.assertEquals(j.Multiply(),8)
        self.assertEquals(j.Divide(),2)

    def testMaxNumber(self):
        """

        :return: 
        """
        j = calculator(9223372036854775808, 9223372036854775808)
        self.assertEquals(j.Multiply(), 850705591730234615865843651865843651857942052864)
        mydata = [[4, 2, 2], [2, 4, -2], [4, 4, 0]]
        n = 0
        for i in mydata:
            j = calculator(mydata[n][0], mydata[n][1])
            self.assertEquals(j.Multiply(), mydata[n][2])
            n += 1

    def testMultiply(self):
        """

        :return: 
        """
        mydata = [[4, 2, 8], [4, -2, -8], [-4, -4, 16],[-5,5,-25]]
        n = 0
        judge = True
        for i in mydata:
            j = calculator(mydata[n][0], mydata[n][1])
            self.assertEquals(j.Multiply(), mydata[n][2])
            n += 1

    def testDivide(self):
        """

        :return: 
        """
        j = calculator(4, 0)
        self.assertEquals(j.Divide(),0)

    def tearDown(self) -> None:
        """

        :return: 
        """
        print('Test End')

    def main(self, out=sys.stderr, verbosity=2):
        """

        :param out: 
        :param verbosity: 
        :return: 
        """
        loader = unittest.TestLoader()

        suite = loader.loadTestsFromModule(sys.modules[__name__])
        unittest.TextTestRunner(out, verbosity=verbosity).run(suite)
        


class calulatorTestCase(unittest.TestCase):
    """

    """
    cals = None

    @classmethod
    def setUpClass(cls):
        """

        :return:
        """
        cls.cals = calculator(4,2)

    def testcaseBase(self):
        """

        :return:
        """
        self.cals = calculator(4, 2)
        self.cals.Add()
        self.cals.Subs()
        self.cals.Multiply()
        self.cals.Divide()
        self.cals.Add = MagicMock()
        self.cals.Subs = MagicMock()
        self.cals.Multiply = MagicMock()
        self.cals.Divide = MagicMock()
        print('Add')

    def testcaseMaxNumber(self):
        """

        :return:
        """
        self.cals=calculator(9223372036854775808, 9223372036854775808)
        self.cals.Multiply()
        self.cals.Multiply = MagicMock()
        print('Subs')

    def testcaseMultiply(self):
        """

        :return:
        """
        mydata = [[4, 2, 8], [4, -2, -8], [-4, -4, 16], [-5, 5, -25]]
        n = 0
        judge = True
        for i in mydata:
            self.cals = calculator(mydata[n][0], mydata[n][1])            
            self.cals.Multiply()
            self.cals.Multiply = MagicMock()
            n += 1

        print('Multiply')

    def testcaseDivide(self):
        """

        :return:
        """
        self.cals = calculator(4, 0)
        self.cals.Divide()
        self.cals.Divide = MagicMock()
        print('Divide')


    @classmethod
    def tearDownClass(cls):
        """
        """
        cls.cal()

    @classmethod
    def main(cls, out=sys.stderr, verbosity=2):
        """

        :param out:
        :param verbosity:
        :return:
        """
        loader = unittest.TestLoader()

        suite = loader.loadTestsFromModule(sys.modules[__name__])
        unittest.TextTestRunner(out, verbosity=verbosity).run(suite)

async def writehtml():
    """
    寫入報告 html
    """       
    suite = unittest.TestSuite()
    suite.addTest(calulatorUnittest("testBase"))
    suite.addTest(calulatorUnittest("testMaxNumber"))
    suite.addTest(calulatorUnittest("testMultiply"))
    suite.addTest(calulatorUnittest("testDivide"))
    
    suite.addTest(calulatorTestCase("testcaseBase"))
    suite.addTest(calulatorTestCase("testcaseMaxNumber"))
    suite.addTest(calulatorTestCase("testcaseMultiply"))
    suite.addTest(calulatorTestCase("testcaseDivide"))
    
    # 1
    #runner = unittest.TextTestRunner()
    #runner.run(suite)
    # 2        
    filepath='testcal.html'
    fp=open(filepath,'wb')
    #寫入報告 html
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'cal.py test report',author='涂聚文',description=u'unit test case')
    runner.run(suite)
    fp.close()
    webbrowser.open('testcal.html',new=0, autoraise=True)  #open html file
    
    
    
async def writetext():
    """
    寫入報告 txt
    """
    if os.path.exists('testcal.txt'):
         with open('testcal.txt', 'w') as f:
            t = calulatorUnittest()
            t.main(f)
    else:
        with open('testcal.txt', 'a') as f:
            t = calulatorTestCase()
            t.main(f)
    webbrowser.open('testcal.txt',new=0, autoraise=True)
    
    unittest.main()
    
    
    
if __name__ == '__main__':
    """

    """
    yn=input("write html file: Y/N?")
    if yn=='Y' or yn=='y':
         # 寫入報告 html
         asyncio.run(writehtml())
    else:
        # 寫入報告 txt
         asyncio.run(writetext())

    

  

 

 

posted @ 2024-10-17 18:08  ®Geovin Du Dream Park™  阅读(9)  评论(0编辑  收藏  举报