This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
conftest.py
281 lines (225 loc) · 8.09 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import os
import shutil
import random
import string
from types import SimpleNamespace
from subprocess import Popen, PIPE, STDOUT
import pytest
from golem.cli import commands
from golem.core import settings_manager, suite, test, session, page
# FIXTURES
BASEDIR = None
def get_basedir():
global BASEDIR
if not BASEDIR:
BASEDIR = os.getcwd()
return BASEDIR
@pytest.fixture(scope="function")
def dir_function():
"""An empty directory"""
basedir = get_basedir()
dirname = TestUtils().random_string(4, 'tempdir_')
path = os.path.join(basedir, dirname)
os.mkdir(path)
os.chdir(path)
directory = SimpleNamespace(name=dirname, path=path, basedir=basedir)
yield directory
os.chdir(basedir)
shutil.rmtree(dirname, ignore_errors=True)
class TestDirectory:
"""Creates and and removes a test directory"""
def __init__(self, basedir):
self.basedir = basedir
self.name = TestUtils.random_numeric_string(6, 'testdir_')
self.path = os.path.join(self.basedir, self.name)
self.settings = None
session.testdir = self.path
commands.createdirectory_command(self.path)
def activate(self):
session.testdir = self.path
if self.settings is None:
self.settings = settings_manager.get_global_settings()
session.settings = self.settings
return self.path
def remove(self):
os.chdir(self.basedir)
shutil.rmtree(self.name, ignore_errors=True)
@pytest.fixture(scope="session")
def testdir_session():
"""A test directory with scope session"""
basedir = get_basedir()
testdir = TestDirectory(basedir)
yield testdir
testdir.remove()
@pytest.fixture(scope="class")
def testdir_class():
"""A test directory with scope class"""
basedir = get_basedir()
testdir = TestDirectory(basedir)
yield testdir
testdir.remove()
@pytest.fixture(scope="function")
def testdir_function():
"""A test directory with scope function"""
basedir = get_basedir()
testdir = TestDirectory(basedir)
yield testdir
testdir.remove()
class Project:
"""Creates and removes a project inside a test directory"""
def __init__(self, testdir_fixture):
self.testdir_fixture = testdir_fixture
self.testdir = testdir_fixture.path
self.name = TestUtils.random_numeric_string(6, 'project_')
self.path = os.path.join(testdir_fixture.path, 'projects', self.name)
self.settings = None
session.testdir = self.testdir
commands.createproject_command(self.name)
def values(self):
return self.testdir, self.name
def activate(self):
session.testdir = self.testdir
if self.settings is None:
self.settings = settings_manager.get_project_settings(self.name)
session.settings = self.settings
return self.values()
def remove(self):
shutil.rmtree(self.path, ignore_errors=True)
@pytest.mark.usefixtures("testdir_session")
@pytest.fixture(scope="session")
def project_session(testdir_session):
"""A project with scope session"""
project = Project(testdir_session)
yield project
project.remove()
@pytest.mark.usefixtures("testdir_session")
@pytest.fixture(scope="class")
def project_class(testdir_session):
"""A project with scope class inside
a test directory with scope session.
"""
project = Project(testdir_session)
yield project
project.remove()
@pytest.mark.usefixtures("testdir_session")
@pytest.fixture(scope="function")
def project_function(testdir_session):
"""A project with scope function inside
a test directory with scope session.
"""
project = Project(testdir_session)
yield project
project.remove()
@pytest.mark.usefixtures("testdir_function")
@pytest.fixture(scope="function")
def project_function_clean(testdir_function):
"""A project with scope function inside
a session with scope function.
"""
project = Project(testdir_function)
yield project
project.remove()
@pytest.fixture(scope="session")
def test_utils():
yield TestUtils
# TEST UTILS
class TestUtils:
@staticmethod
def create_empty_file(path, filename):
filepath = os.path.join(path, filename)
os.makedirs(path, exist_ok=True)
open(filepath, 'w+').close()
@staticmethod
def random_string(length=10, prefix=''):
random_str = (''.join(random.choice(string.ascii_lowercase)
for _ in range(length)))
return prefix + random_str
@staticmethod
def random_email():
local = TestUtils.random_string(10)
domain = TestUtils.random_string(10)
return '{}@{}.com'.format(local, domain)
@staticmethod
def random_numeric_string(length, prefix=''):
random_str = ''.join(random.choice(string.digits) for _ in range(length))
return prefix + random_str
@staticmethod
def run_command(cmd):
output = ''
p = Popen(cmd, stdout=PIPE, stderr=STDOUT, bufsize=0,
shell=True, universal_newlines=True)
output = p.stdout.read()
if len(output) > 1 and output[-1] == '\n':
output = output[:-1]
return output
@staticmethod
def set_project_setting(testdir, setting, setting_value):
setting_path = os.path.join(testdir, 'settings.json')
with open(setting_path, 'w') as f:
f.write('{{"{}": "{}"\}}'.format(setting, setting_value))
@staticmethod
def create_test(project, name, content=None):
if content is None:
content = ('def test(data):\n'
' print("hello")\n')
test.create_test(project, name)
test.edit_test_code(project, name, content, table_test_data=[])
return test.Test(project, name).path
@staticmethod
def create_random_test(project):
test_name = TestUtils.random_string(10)
test.create_test(project, test_name)
return test_name
@staticmethod
def create_suite(project, name, content=None, tests=None,
processes=1, browsers=None, environments=None, tags=None):
browsers = browsers or []
environments = environments or []
tags = tags or []
if content is None:
suite.create_suite(project, name)
suite.edit_suite(project, name, tests, processes, browsers, environments, tags)
else:
with open(suite.Suite(project, name).path, 'w+') as f:
f.write(content)
@staticmethod
def create_page(project, page_name):
page.create_page(project, page_name)
@staticmethod
def create_random_suite(project):
suite_name = TestUtils.random_string()
suite.create_suite(project, suite_name)
return suite_name
@staticmethod
def create_random_page(project, code=None):
page_name = TestUtils.random_string(10)
page.create_page(project, page_name)
if code is not None:
page.edit_page_code(project, page_name, code)
return page_name
def pytest_addoption(parser):
parser.addoption('--integration', action='store_true', help='run integration tests only')
parser.addoption('--fast', action='store_true', help='run integration tests only')
parser.addoption('--all', action='store_true', help='run all tests')
def pytest_runtest_setup(item):
"""Filter tests
'' (no option): Run all non integration tests
'--integration' Run integration tests only
'--fast': Run fast tests only
'--all': Run all tests
"""
if item.config.getoption('--all'):
pass # run all tests
else:
if item.config.getoption('--integration'):
# skip non integration tests
if 'integration' not in item.keywords:
pytest.skip('skipping non integration test')
else:
# skip integration tests
if 'integration' in item.keywords:
pytest.skip('skipping integration test')
if item.config.getoption('--fast'):
# skip slow test
if 'slow' in item.keywords:
pytest.skip('skipping slow test')