Skip to content

Instantly share code, notes, and snippets.

@hackebrot
Last active July 6, 2017 20:28
Show Gist options
  • Save hackebrot/86c55e08d7de2126c75b141a7b47de2f to your computer and use it in GitHub Desktop.
Save hackebrot/86c55e08d7de2126c75b141a7b47de2f 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!"
@luzfcb
Copy link

luzfcb commented Jul 6, 2017

@pytest.fixture(scope='session')
def browser(browser):
    url = "http://127.0.0.1:8000/login/"

    browser.visit(url)
    browser.fill('username', 'foobar_user')
    browser.fill('password', 'foobar_user')
    button = browser.find_by_xpath('//*[@id="sign-in"]/div[4]/input')
    button.click()

    return browser

Did not work, I got this error

=============================================================== ERRORS ===============================================================
_______________________________________________ ERROR at setup of test_mensagens_erro ________________________________________________
ScopeMismatch: You tried to access the 'function' scoped fixture 'browser' with a u'session' scoped request object, involved factories
tests/test_cadastro_servidor.py:70:  def browser(browser)
../../virtualenvs/solar/lib/python2.7/site-packages/pytest_splinter/plugin.py:578:  def browser(request, browser_instance_getter)
___________________________________________________ ERROR at setup of test_foobar ____________________________________________________
ScopeMismatch: You tried to access the 'function' scoped fixture 'browser' with a u'session' scoped request object, involved factories
tests/test_cadastro_servidor.py:70:  def browser(browser)
../../virtualenvs/solar/lib/python2.7/site-packages/pytest_splinter/plugin.py:578:  def browser(request, browser_instance_getter)
====================================================== 2 error in 0.01 seconds =======================================================

@luzfcb
Copy link

luzfcb commented Jul 6, 2017

Solved

@pytest.fixture(scope='session')
def session_browser(session_browser):
    url = "http://127.0.0.1:8000/login/"

    session_browser.visit(url)
    session_browser.fill('username', 'foobar_user')
    session_browser.fill('password', 'foobar_user')
    button = session_browser.find_by_xpath('//*[@id="sign-in"]/div[4]/input')
    button.click()

    return session_browser

def test_foobar_case1(session_browser):
        assert True

def test_foobar_case2(session_browser):
        assert True

It's easier than I thought :-) 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment