Skip to content

Commit e814bcd

Browse files
committed
pytest autouse fixtures
1 parent 0c5b615 commit e814bcd

File tree

7 files changed

+97
-16
lines changed

7 files changed

+97
-16
lines changed

case/argparse/conftest.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,3 @@ def _temp_file():
2424
print('the current temp path is {}'.format(temp))
2525
yield temp
2626
os.remove(temp)
27-
28-
29-
@pytest.fixture()
30-
def cleandir():
31-
''' cleandir '''
32-
newpath = tempfile.mkdtemp()
33-
os.chdir(newpath)
34-
print('\nthe current temp dir is {}'.format(newpath))
35-
36-
37-
# @pytest.mark.xfail(raises=ZeroDivisionError)
38-
# def test_zero_division():
39-
# ''' test zero division'''
40-
# 1/0
41-
# with pytest.raises(ZeroDivisionError):
42-
# 1 / 0

case/pytest/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding:utf-8 -*-

case/pytest/conftest.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import os
4+
import shutil
5+
import tempfile
6+
import pytest
7+
8+
9+
@pytest.fixture(scope="session")
10+
def _temp_dir():
11+
''' _temp_dir '''
12+
temp = tempfile.mkdtemp(prefix='argprase-')
13+
print('\n')
14+
print('the current temp dir is {}'.format(temp))
15+
yield temp
16+
shutil.rmtree(temp)
17+
18+
19+
@pytest.fixture(scope="session")
20+
def _temp_file():
21+
''' _temp_file '''
22+
fd, temp = tempfile.mkstemp(prefix='argprase-', suffix='.tmp')
23+
# print('\n')
24+
print('the current temp path is {}'.format(temp))
25+
yield temp
26+
os.remove(temp)
27+
28+
29+
@pytest.fixture()
30+
def cleandir():
31+
''' cleandir '''
32+
newpath = tempfile.mkdtemp()
33+
os.chdir(newpath)
34+
print('\nthe current temp dir is {}'.format(newpath))
35+
36+
37+
# @pytest.mark.xfail(raises=ZeroDivisionError)
38+
# def test_zero_division():
39+
# ''' test zero division'''
40+
# 1/0
41+
# with pytest.raises(ZeroDivisionError):
42+
# 1 / 0

case/pytest/test_db_transact.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import os
4+
import pytest
5+
6+
7+
class DB(object):
8+
''' db '''
9+
def __init__(self):
10+
self.intransaction = []
11+
12+
def begin(self, name):
13+
''' begin '''
14+
print('\nDB::begin: {}'.format(name))
15+
self.intransaction.append(name)
16+
17+
def rollback(self):
18+
''' rollback '''
19+
item = self.intransaction.pop()
20+
print('\nDB::rollback: {}'.format(item))
21+
22+
23+
@pytest.fixture(scope="module")
24+
def db():
25+
''' db '''
26+
return DB()
27+
28+
29+
class TestClass(object):
30+
''' TestClass '''
31+
@pytest.fixture(autouse=True)
32+
def transact(self, request, db):
33+
''' transact '''
34+
db.begin(request.function.__name__)
35+
yield
36+
db.rollback()
37+
38+
def test_method1(self, db):
39+
''' test_method1 '''
40+
print('\nTestClass::test_method1: {}'.format(db))
41+
assert db.intransaction == ["test_method1"]
42+
43+
def test_method2(self, db):
44+
''' test_method2 '''
45+
print('\nTestClass::test_method2: {}'.format(db))
46+
assert db.intransaction == ["test_method2"]
47+
48+
49+
if __name__ == "__main__":
50+
''' the main point '''
51+
retcode = pytest.main(args=['-v', '-s', os.path.abspath(__file__)])
52+
# retcode = pytest.main(args=['--fixtures', os.path.abspath(__file__)])
53+
# retcode = pytest.main(args=['--collect-only', os.path.abspath(__file__)])
54+
print(retcode)

0 commit comments

Comments
 (0)