# unittest

***

```python
# 1. Testing an assertion
import unittest

class MyTestCase(unittest.TestCase):
    def test_something(self):
        self.assertEqual(True, True)  # passes
```

```python
# 2. Testing a function
import unittest

def my_function(x):
    return x * 2

class MyTestCase(unittest.TestCase):
    def test_my_function(self):
        self.assertEqual(my_function(2), 4)  # passes
```

```python
# 3. Testing a method
import unittest

class MyTestClass:
    def my_method(self, x):
        return x * 2

class MyTestCase(unittest.TestCase):
    def test_my_method(self):
        my_test_class = MyTestClass()
        self.assertEqual(my_test_class.my_method(2), 4)  # passes
```

```python
# 4. Testing a class
import unittest

class MyTestClass:
    def __init__(self, x):
        self.x = x

    def my_method(self):
        return self.x * 2

class MyTestCase(unittest.TestCase):
    def test_my_class(self):
        my_test_class = MyTestClass(2)
        self.assertEqual(my_test_class.my_method(), 4)  # passes
```

```python
# 5. Testing a generator
import unittest

def my_generator():
    yield 1
    yield 2
    yield 3

class MyTestCase(unittest.TestCase):
    def test_my_generator(self):
        self.assertEqual(list(my_generator()), [1, 2, 3])  # passes
```

```python
# 6. Testing a context manager
import unittest

class MyContextManager:
    def __enter__(self):
        return 'foo'

    def __exit__(self, exc_type, exc_value, traceback):
        pass

class MyTestCase(unittest.TestCase):
    def test_my_context_manager(self):
        with MyContextManager() as foo:
            self.assertEqual(foo, 'foo')  # passes
```

```python
# 7. Testing a decorator
import unittest

def my_decorator(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs) + 1
    return wrapper

class MyTestCase(unittest.TestCase):
    def test_my_decorator(self):
        @my_decorator
        def my_function(x):
            return x
        self.assertEqual(my_function(2), 3)  # passes
```

```python
# 8. Testing a classmethod
import unittest

class MyTestClass:
    @classmethod
    def my_classmethod(cls, x):
        return x * 2

class MyTestCase(unittest.TestCase):
    def test_my_classmethod(self):
        self.assertEqual(MyTestClass.my_classmethod(2), 4)  # passes
```

```python
# 9. Testing a staticmethod
import unittest

class MyTestClass:
    @staticmethod
    def my_staticmethod(x):
        return x * 2

class MyTestCase(unittest.TestCase):
    def test_my_staticmethod(self):
        self.assertEqual(MyTestClass.my_staticmethod(2), 4)  # passes
```

```python
# 10. Testing a property
import unittest

class MyTestClass:
    @property
    def my_property(self):
        return 'foo'

class MyTestCase(unittest.TestCase):
    def test_my_property(self):
        my_test_class = MyTestClass()
        self.assertEqual(my_test_class.my_property, 'foo')  # passes
```
