jeudi 1 octobre 2015

DRY in Unit Test for Python

From two files test_now.py and test_later.py as follows:

# test_now.py
import unittest

class CommonClass(unittest.TestCase):
    def hello(self):
        print "Hello there"
    def bye(self):
        print "Bye"
    def seeYouAgain(self):
        print "See You Again"
    def whatsUp(self):
        print "What's up?"

    def testNow(self):
        self.hello()
        self.bye()

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

# test_later.py
import unittest

class CommonClass(unittest.TestCase):
    def hello(self):
        print "Hello there"
    def bye(self):
        print "Bye"
    def seeYouAgain(self):
        print "See You Again"
    def whatsUp(self):
        print "What's up?"

    def testLater(self):
        self.hello()
        self.whatsUp()

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

I reorganize in three files as follows:

# common_class.py
import unittest

class CommonClass(unittest.TestCase):
    def hello(self):
        print "Hello there"
    def bye(self):
        print "Bye"
    def seeYouAgain(self):
        print "See You Again"
    def whatsUp(self):
        print "What's up?"  

# test_now.py
from common_class import *
def testNow(self)
    self.hello()
    self.bye()
setattr(CommonClass, 'testNow', testNow)

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

# test_later.py
from common_class import *
def testLater(self):
    self.hello()
    self.whatsUp()
setattr(CommonClass, 'testLater', testLater)

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

What are the concerns about this DRY approach?

Aucun commentaire:

Enregistrer un commentaire