lundi 25 avril 2016

DI with unit tests called from elsewhere in application?

I am attempting to do something similar to the following:

import unittest

class foo:
    one = 1
    two = 1

class bar:
    one = 2
    two = 2


class my_test(unittest.TestCase):

    def __init__(self, di_source):
        self.di = di_source
        print 'initing my_test'

    def setUp(self):
        print 'setting up!'

    def tearDown(self):
        print 'tearing down :('

    def test_case_one(self):
        self.assertEqual(self.di.one,1)

    def test_case_two(self):
        self.assertEqual(self.di.two, 2)


di_one = foo()
di_two = bar()

# called from elsewhere in my application
test_one = my_test(di_one).run()
test_one = my_test(di_two).run()

My goal is to:

  • Be able to call run() on a test suite
  • Provide a DI container at runtime to that test suite
  • Take advantage of the setUp and tearDown functionality provided by the unit test framework

However, it seems when I attempt to do this that the unittest framework doesn't like my constructor:

AttributeError: 'my_test' object has no attribute '_testMethodName'

Is there a better way to structure this example to avoid this problem?

Aucun commentaire:

Enregistrer un commentaire