Skip to content

Commit 17f52d2

Browse files
committed
test: remove test inter-dependencies
A bunch of tests had interdependencies since we rely on chdir'ing to tempdirs (and then deleting those dirs) for various tests -- by running the tests in randomized orders, we'd get randomized failures as it turns out we were depending on eg. the existence of a previously-generated .coverage file or being in a still-existing directory. None of these randomized failures were actual functional issues, but this should improve the consistency of our test runs in the future.
1 parent e03a2de commit 17f52d2

5 files changed

Lines changed: 87 additions & 49 deletions

File tree

tests/api/configuration_test.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# pylint: disable=no-self-use
22
import os
3-
import shutil
43
import tempfile
54
import unittest
65
from unittest import mock
@@ -17,17 +16,21 @@
1716

1817
@mock.patch.object(Coveralls, 'config_filename', '.coveralls.mock')
1918
class Configuration(unittest.TestCase):
19+
@classmethod
20+
def setUpClass(cls):
21+
cls.old_cwd = os.getcwd()
22+
23+
@classmethod
24+
def tearDownClass(cls):
25+
os.chdir(cls.old_cwd)
26+
2027
def setUp(self):
2128
self.dir = tempfile.mkdtemp()
22-
2329
os.chdir(self.dir)
2430
with open('.coveralls.mock', 'w+') as fp:
2531
fp.write('repo_token: xxx\n')
2632
fp.write('service_name: jenkins\n')
2733

28-
def tearDown(self):
29-
shutil.rmtree(self.dir)
30-
3134
@pytest.mark.skipif(yaml is None, reason='requires PyYAML')
3235
@mock.patch.dict(os.environ, {}, clear=True)
3336
def test_local_with_config(self):

tests/api/encoding_test.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import subprocess
4+
import unittest
45

56
from coveralls import Coveralls
67

@@ -9,28 +10,40 @@
910
NONUNICODE_DIR = os.path.join(BASE_DIR, 'nonunicode')
1011

1112

12-
def test_non_unicode():
13-
os.chdir(NONUNICODE_DIR)
14-
subprocess.call(['coverage', 'run', 'nonunicode.py'], cwd=NONUNICODE_DIR)
15-
16-
actual_json = json.dumps(Coveralls(repo_token='xxx').get_coverage())
17-
expected_json_part = (
18-
'"source": "# coding: iso-8859-15\\n\\n'
19-
'def hello():\\n'
20-
' print(\'I like P\\u00f3lya distribution.\')')
21-
assert expected_json_part in actual_json
22-
23-
24-
def test_malformed_encoding_declaration_py3_or_coverage4():
25-
os.chdir(NONUNICODE_DIR)
26-
subprocess.call(['coverage', 'run', 'malformed.py'], cwd=NONUNICODE_DIR)
27-
28-
result = Coveralls(repo_token='xxx').get_coverage()
29-
assert len(result) == 1
30-
31-
assert result[0]['coverage'] == [None, None, 1, 0]
32-
assert result[0]['name'] == 'malformed.py'
33-
assert result[0]['source'].strip() == ('# -*- cоding: utf-8 -*-\n\n'
34-
'def hello():\n'
35-
' return 1')
36-
assert 'branches' not in result[0]
13+
class EncodingTest(unittest.TestCase):
14+
@classmethod
15+
def setUpClass(cls):
16+
cls.old_cwd = os.getcwd()
17+
18+
@classmethod
19+
def tearDownClass(cls):
20+
os.chdir(cls.old_cwd)
21+
22+
@staticmethod
23+
def test_non_unicode():
24+
os.chdir(NONUNICODE_DIR)
25+
subprocess.call(['coverage', 'run', 'nonunicode.py'],
26+
cwd=NONUNICODE_DIR)
27+
28+
actual_json = json.dumps(Coveralls(repo_token='xxx').get_coverage())
29+
expected_json_part = (
30+
'"source": "# coding: iso-8859-15\\n\\n'
31+
'def hello():\\n'
32+
' print(\'I like P\\u00f3lya distribution.\')')
33+
assert expected_json_part in actual_json
34+
35+
@staticmethod
36+
def test_malformed_encoding_declaration_py3_or_coverage4():
37+
os.chdir(NONUNICODE_DIR)
38+
subprocess.call(['coverage', 'run', 'malformed.py'],
39+
cwd=NONUNICODE_DIR)
40+
41+
result = Coveralls(repo_token='xxx').get_coverage()
42+
assert len(result) == 1
43+
44+
assert result[0]['coverage'] == [None, None, 1, 0]
45+
assert result[0]['name'] == 'malformed.py'
46+
assert result[0]['source'].strip() == ('# -*- cоding: utf-8 -*-\n\n'
47+
'def hello():\n'
48+
' return 1')
49+
assert 'branches' not in result[0]

tests/api/reporter_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def assert_coverage(actual, expected):
1818

1919

2020
class ReporterTest(unittest.TestCase):
21+
@classmethod
22+
def setUpClass(cls):
23+
cls.old_cwd = os.getcwd()
24+
25+
@classmethod
26+
def tearDownClass(cls):
27+
os.chdir(cls.old_cwd)
28+
2129
def setUp(self):
2230
os.chdir(EXAMPLE_DIR)
2331

tests/git_test.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# pylint: disable=no-self-use
22
import os
33
import re
4-
import shutil
54
import subprocess
65
import tempfile
76
import unittest
@@ -18,10 +17,18 @@
1817

1918

2019
class GitTest(unittest.TestCase):
20+
@classmethod
21+
def setUpClass(cls):
22+
cls.old_cwd = os.getcwd()
23+
24+
@classmethod
25+
def tearDownClass(cls):
26+
os.chdir(cls.old_cwd)
27+
2128
def setUp(self):
2229
self.dir = tempfile.mkdtemp()
23-
2430
os.chdir(self.dir)
31+
2532
# TODO: switch to pathlib
2633
open('README', 'a').close() # pylint: disable=consider-using-with
2734

@@ -35,9 +42,6 @@ def setUp(self):
3542
subprocess.call(['git', 'remote', 'add', GIT_REMOTE, GIT_URL],
3643
cwd=self.dir)
3744

38-
def tearDown(self):
39-
shutil.rmtree(self.dir)
40-
4145
@mock.patch.dict(os.environ, {'TRAVIS_BRANCH': 'master'}, clear=True)
4246
def test_git(self):
4347
git_info = coveralls.git.git_info()
@@ -74,7 +78,19 @@ def test_gitlog(self):
7478
assert coveralls.git.gitlog('%s') == GIT_COMMIT_MSG
7579

7680

77-
class GitInfoTestEnvVars(unittest.TestCase):
81+
class GitInfoTest(unittest.TestCase):
82+
@classmethod
83+
def setUpClass(cls):
84+
cls.old_cwd = os.getcwd()
85+
86+
@classmethod
87+
def tearDownClass(cls):
88+
os.chdir(cls.old_cwd)
89+
90+
def setUp(self):
91+
self.dir = tempfile.mkdtemp()
92+
os.chdir(self.dir)
93+
7894
@mock.patch.dict(os.environ, {
7995
'GIT_ID': '5e837ce92220be64821128a70f6093f836dd2c05',
8096
'GIT_BRANCH': 'master',
@@ -86,7 +102,7 @@ class GitInfoTestEnvVars(unittest.TestCase):
86102
'GIT_URL': GIT_URL,
87103
'GIT_REMOTE': GIT_REMOTE,
88104
}, clear=True)
89-
def test_gitlog_envvars(self):
105+
def test_gitinfo_envvars(self):
90106
git_info = coveralls.git.git_info()
91107
commit_id = git_info['git']['head'].pop('id')
92108
assert re.match(r'^[a-f0-9]{40}$', commit_id)
@@ -108,24 +124,14 @@ def test_gitlog_envvars(self):
108124
},
109125
}
110126

111-
112-
class GitInfoTestNotAGitRepository(unittest.TestCase):
113-
def setUp(self):
114-
self.dir = tempfile.mkdtemp()
115-
116-
os.chdir(self.dir)
117-
118-
def tearDown(self):
119-
shutil.rmtree(self.dir)
120-
121-
def test_gitlog_not_a_git_repo(self):
127+
def test_gitinfo_not_a_git_repo(self):
122128
git_info = coveralls.git.git_info()
123129

124130
self.assertRaises(CoverallsException)
125131
assert git_info == {}
126132

127133

128-
class GitInfoTestBranch(GitTest):
134+
class GitInfoOverridesTest(unittest.TestCase):
129135
@mock.patch.dict(os.environ, {
130136
'GITHUB_ACTIONS': 'true',
131137
'GITHUB_REF': 'refs/pull/1234/merge',

tests/integration_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class IntegrationTest(unittest.TestCase):
2828
'GIT_MESSAGE': 'Ran the integration tests',
2929
}
3030

31+
@classmethod
32+
def setUpClass(cls):
33+
cls.old_cwd = os.getcwd()
34+
35+
@classmethod
36+
def tearDownClass(cls):
37+
os.chdir(cls.old_cwd)
38+
3139
def _test_harness(self, num, hits):
3240
with tempfile.TemporaryDirectory() as tempdir:
3341
os.chdir(tempdir)

0 commit comments

Comments
 (0)