jeudi 10 septembre 2015

How to make unit tests for a methods in complicated class in python?

I would like to create unit test for filename_to_txt method:

class some_panel(wx.Panel):
    def __init__(self,parent,Some_class,some_handler,Some_Event):
        wx.Panel.__init__(self,parent=parent)
        self.parent = parent
        self.some_handler = some_handler
        self.some_Event = Some_Event
        self.some_another_class = Some_class

    def filename_to_txt(self,input_filename):
        splitted = input_filename.split(".raw")
        txt_filename  = splitted[0] + splitted[1] + ".txt"
        return txt_filename

How should I write unit test for that method?

It is static method in some class, but to test that firstly I have to make and instance of Some_panel class.

Do I really have to provide all arguments for __init__ method of Some_panel class?? Is there another better approach, for which I don't have to make an instance of that Some_panel class to test filename_to_txt method.

import unittest
from some_file import Some_panel

class TestSomething(unittest.TestCase):
    def testname(self):
        some_panel = Some_panel( ???? )
        testfilename = "TestFilename.raw.001"
        result = some_panel.filename_to_txt(input_filename = testfilename)
        self.assertEqual(result, "TestFilename.001.txt", "Something is wrong")

Aucun commentaire:

Enregistrer un commentaire