Skip to content

Instantly share code, notes, and snippets.

@jrast
Created July 8, 2014 19:43
Show Gist options
  • Save jrast/109f70f9b4c52bab4252 to your computer and use it in GitHub Desktop.
Save jrast/109f70f9b4c52bab4252 to your computer and use it in GitHub Desktop.
Nose2: such and unittest.TestCase
# -*- coding: utf-8 -*-
if __name__ == '__main__':
import logging
import nose2
logging.basicConfig(filename="such.log", level=logging.INFO)
nose2.discover()
--- With Exception in UniqueResource
INFO:root:Called setUpClass in NormalTest
INFO:root:Called tearDownClass in NormalTest
INFO:root:Called setUpClass in NormalTestTwo
INFO:root:Called setup in such test
--- Exception commented out in UniqueResource
INFO:root:Called setUpClass in NormalTest
INFO:root:Called tearDownClass in NormalTest
INFO:root:Called setUpClass in NormalTestTwo
INFO:root:Called setup in such test
INFO:root:Called teardown in such test
INFO:root:Called tearDownClass in NormalTestTwo
# -*- coding: utf-8 -*-
import unittest
from nose2.tools import such
from unique_resource import UniqueResource
import logging
log = logging.getLogger()
class NormalTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
log.info("Called setUpClass in NormalTest")
cls.unique_resource = UniqueResource()
cls.unique_resource.lock()
@classmethod
def tearDownClass(cls):
log.info("Called tearDownClass in NormalTest")
cls.unique_resource.unlock()
def test(self):
self.assertTrue(self.unique_resource.used)
class NormalTestTwo(unittest.TestCase):
@classmethod
def setUpClass(cls):
log.info("Called setUpClass in NormalTestTwo")
cls.unique_resource = UniqueResource()
cls.unique_resource.lock()
@classmethod
def tearDownClass(cls):
log.info("Called tearDownClass in NormalTestTwo")
cls.unique_resource.unlock()
def test(self):
self.assertTrue(self.unique_resource.used)
# -*- coding: utf-8 -*-
from nose2.tools import such
from unique_resource import UniqueResource
import logging
log = logging.getLogger()
with such.A('system with setup') as it:
@it.has_setup
def setup():
log.info("Called setup in such test")
it.unique_resource = UniqueResource()
it.unique_resource.lock()
@it.has_teardown
def teardown():
log.info("Called teardown in such test")
it.unique_resource.unlock()
@it.should('do something')
def test(case):
it.assertTrue(it.unique_resource.used)
it.createTests(globals())
# -*- coding: utf-8 -*-
class UniqueResource(object):
_instance = None
used = False
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(UniqueResource, cls).__new__(cls, *args, **kwargs)
return cls._instance
def lock(self):
if not self.used:
self.used = True
else:
raise Exception("Resource allready used")
def unlock(self):
self.used = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment