Skip to content

Instantly share code, notes, and snippets.

@luzfcb
Forked from hackebrot/README.md
Created July 6, 2017 20:14
Show Gist options
  • Save luzfcb/42a7522365ae5595fd76e1a3f9f82f65 to your computer and use it in GitHub Desktop.
Save luzfcb/42a7522365ae5595fd76e1a3f9f82f65 to your computer and use it in GitHub Desktop.
Python class setup

Run pytest and show prints:

$ pytest -v -s
test_helloworld.py::TestHello::test_python [TestHello.setUpClass] PASSED
test_helloworld.py::TestHello::test_world PASSED
test_helloworld.py::TestHey::test_world [TestHey.setup_class] PASSED
test_helloworld.py::TestHey::test_python PASSED
test_helloworld.py::TestHi::test_world [greet fixture] PASSED
test_helloworld.py::TestHi::test_python PASSED
# -*- coding: utf-8 -*-
import unittest
import pytest
class TestHello(unittest.TestCase):
@classmethod
def setUpClass(cls):
print(f'[{cls.__name__}.setUpClass]')
def hello(self, r):
return f'Hello {r}!'
cls.greet = hello
def test_world(self):
self.assertEqual(self.greet("World"), "Hello World!")
def test_python(self):
self.assertEqual(self.greet("Python"), "Hello Python!")
class TestHey:
@classmethod
def setup_class(cls):
print(f'[{cls.__name__}.setup_class]')
def hey(self, r):
return f'Hey {r}!'
cls.greet = hey
def test_world(self):
assert self.greet("World") == "Hey World!"
def test_python(self):
assert self.greet("Python") == "Hey Python!"
@pytest.fixture(scope='class')
def greet():
print(f'[greet fixture]')
def hi(r):
return f'Hi {r}!'
return hi
class TestHi:
def test_world(self, greet):
assert greet("World") == "Hi World!"
def test_python(self, greet):
assert greet("Python") == "Hi Python!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment