Skip to content

Commit 3b7d482

Browse files
committed
Simplify test cleanup
1 parent ae09cc3 commit 3b7d482

5 files changed

Lines changed: 20 additions & 85 deletions

File tree

test/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""The global configuration for the test suite"""
2+
import os
3+
import subprocess
4+
5+
import pytest
6+
7+
HERE = os.path.dirname(os.path.abspath(__file__))
8+
DATAFILES_PATH = os.path.join(HERE, 'data')
9+
10+
11+
# after we have run all tests, use git to delete the built files in data/
12+
@pytest.fixture(scope='session', autouse=True)
13+
def cleanup_test_files():
14+
yield
15+
subprocess.Popen(
16+
['git', 'clean', '-fX', DATAFILES_PATH],
17+
stdout=subprocess.DEVNULL,
18+
stderr=subprocess.STDOUT,
19+
)

test/test_model.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from unittest.mock import Mock
99

1010
import numpy as np
11-
import pytest
1211
import tqdm
1312
from testfixtures import LogCapture
1413

@@ -37,23 +36,6 @@
3736

3837

3938
class CmdStanModelTest(unittest.TestCase):
40-
41-
# pylint: disable=no-self-use
42-
@pytest.fixture(scope='class', autouse=True)
43-
def do_clean_up(self):
44-
for root, _, files in os.walk(DATAFILES_PATH):
45-
for filename in files:
46-
_, ext = os.path.splitext(filename)
47-
if (
48-
ext.lower() in ('.o', '.d', '.hpp', '.exe', '')
49-
and filename != ".gitignore"
50-
and filename != "return_one.hpp"
51-
):
52-
filepath = os.path.join(root, filename)
53-
os.remove(filepath)
54-
# we should really make this module-level
55-
# and use something like git clean -Xf data/
56-
5739
def show_cmdstan_version(self):
5840
print('\n\nCmdStan version: {}\n\n'.format(cmdstan_path()))
5941
self.assertTrue(True)
@@ -94,6 +76,7 @@ def test_model_good(self):
9476
self.assertEqual(BERN_STAN, model.stan_file)
9577
self.assertEqual(None, model.exe_file)
9678

79+
# pylint: disable=no-self-use
9780
def test_model_pedantic(self):
9881
with LogCapture() as log:
9982
logging.getLogger()

test/test_optimize.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import unittest
66

77
import numpy as np
8-
import pytest
98
from testfixtures import LogCapture
109

1110
from cmdstanpy.cmdstan_args import CmdStanArgs, OptimizeArgs
@@ -18,21 +17,6 @@
1817

1918

2019
class CmdStanMLETest(unittest.TestCase):
21-
22-
# pylint: disable=no-self-use
23-
@pytest.fixture(scope='class', autouse=True)
24-
def do_clean_up(self):
25-
for root, _, files in os.walk(DATAFILES_PATH):
26-
for filename in files:
27-
_, ext = os.path.splitext(filename)
28-
if (
29-
ext.lower() in ('.o', '.d', '.hpp', '.exe', '')
30-
and filename != ".gitignore"
31-
and filename != "return_one.hpp"
32-
):
33-
filepath = os.path.join(root, filename)
34-
os.remove(filepath)
35-
3620
def test_instantiate(self):
3721
stan = os.path.join(DATAFILES_PATH, 'optimize', 'rosenbrock.stan')
3822
model = CmdStanModel(stan_file=stan)

test/test_sample.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from time import time
1414

1515
import numpy as np
16-
import pytest
1716
from testfixtures import LogCapture
1817

1918
try:
@@ -56,21 +55,6 @@ def without_import(library, module):
5655

5756

5857
class SampleTest(unittest.TestCase):
59-
60-
# pylint: disable=no-self-use
61-
@pytest.fixture(scope='class', autouse=True)
62-
def do_clean_up(self):
63-
for root, _, files in os.walk(DATAFILES_PATH):
64-
for filename in files:
65-
_, ext = os.path.splitext(filename)
66-
if (
67-
ext.lower() in ('.o', '.d', '.hpp', '.exe', '')
68-
and filename != ".gitignore"
69-
and filename != "return_one.hpp"
70-
):
71-
filepath = os.path.join(root, filename)
72-
os.remove(filepath)
73-
7458
def test_bernoulli_good(self, stanfile='bernoulli.stan'):
7559
stan = os.path.join(DATAFILES_PATH, stanfile)
7660
bern_model = CmdStanModel(stan_file=stan)

test/test_variational.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import unittest
55
from math import fabs
66

7-
import pytest
87
from testfixtures import LogCapture
98

109
from cmdstanpy.cmdstan_args import CmdStanArgs, VariationalArgs
@@ -16,23 +15,6 @@
1615

1716

1817
class CmdStanVBTest(unittest.TestCase):
19-
20-
# pylint: disable=no-self-use
21-
@pytest.fixture(scope='class', autouse=True)
22-
def do_clean_up(self):
23-
for root, _, files in os.walk(
24-
os.path.join(DATAFILES_PATH, 'variational')
25-
):
26-
for filename in files:
27-
_, ext = os.path.splitext(filename)
28-
if (
29-
ext.lower() in ('.o', '.d', '.hpp', '.exe', '')
30-
and filename != ".gitignore"
31-
and filename != "return_one.hpp"
32-
):
33-
filepath = os.path.join(root, filename)
34-
os.remove(filepath)
35-
3618
def test_instantiate(self):
3719
stan = os.path.join(
3820
DATAFILES_PATH, 'variational', 'eta_should_be_big.stan'
@@ -155,23 +137,6 @@ def test_variables_3d(self):
155137

156138

157139
class VariationalTest(unittest.TestCase):
158-
159-
# pylint: disable=no-self-use
160-
@pytest.fixture(scope='class', autouse=True)
161-
def do_clean_up(self):
162-
for root, _, files in os.walk(
163-
os.path.join(DATAFILES_PATH, 'variational')
164-
):
165-
for filename in files:
166-
_, ext = os.path.splitext(filename)
167-
if (
168-
ext.lower() in ('.o', '.d', '.hpp', '.exe', '')
169-
and filename != ".gitignore"
170-
and filename != "return_one.hpp"
171-
):
172-
filepath = os.path.join(root, filename)
173-
os.remove(filepath)
174-
175140
def test_variational_good(self):
176141
stan = os.path.join(
177142
DATAFILES_PATH, 'variational', 'eta_should_be_big.stan'

0 commit comments

Comments
 (0)