Skip to content

Commit ffa1c26

Browse files
committed
Tox and pytest
1 parent 07ef6c3 commit ffa1c26

File tree

6 files changed

+91
-27
lines changed

6 files changed

+91
-27
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ examples/searchcommands_app/package/default/commands.conf
2828
examples/searchcommands_app/package/lib/splunklib
2929
tests/searchcommands/apps/app_with_logging_configuration/*.log
3030
*.observed
31-
venv/
31+
venv/
32+
.tox
33+
test-reports/

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def run_test_suite():
3636
def mark_failed():
3737
global failed
3838
failed = True
39-
39+
4040
class _TrackingTextTestResult(unittest._TextTestResult):
4141
def addError(self, test, err):
4242
unittest._TextTestResult.addError(self, test, err)
@@ -50,14 +50,14 @@ class TrackingTextTestRunner(unittest.TextTestRunner):
5050
def _makeResult(self):
5151
return _TrackingTextTestResult(
5252
self.stream, self.descriptions, self.verbosity)
53-
53+
5454
original_cwd = os.path.abspath(os.getcwd())
5555
os.chdir('tests')
5656
suite = unittest.defaultTestLoader.discover('.')
5757
runner = TrackingTextTestRunner(verbosity=2)
5858
runner.run(suite)
5959
os.chdir(original_cwd)
60-
60+
6161
return failed
6262

6363

@@ -205,7 +205,7 @@ def run(self):
205205
target = os.path.join('build', tarball)
206206

207207
shutil.copyfile(source, target)
208-
208+
209209
return
210210

211211
setup(

tests/searchcommands/test_internals_v1.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424

2525
from contextlib import closing
2626
from splunklib.six.moves import cStringIO as StringIO
27-
from itertools import izip
27+
28+
try:
29+
from itertools import izip # python 2
30+
except ImportError:
31+
from builtins import zip as izip # python 3
32+
2833
from unittest import main, TestCase
2934

3035
import os

tests/searchcommands/test_searchcommands_app.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,22 @@
2929
from collections import namedtuple
3030
from splunklib.six.moves import cStringIO as StringIO
3131
from datetime import datetime
32-
from itertools import ifilter, imap, izip
32+
33+
try:
34+
from itertools import ifilter # python 2
35+
except ImportError:
36+
from builtins import filter as ifilter # python 3
37+
38+
try:
39+
from itertools import imap # python 2
40+
except ImportError:
41+
from builtins import map as imap # python 3
42+
43+
try:
44+
from itertools import izip # python 2
45+
except ImportError:
46+
from builtins import zip as izip # python 3
47+
3348
from subprocess import PIPE, Popen
3449
from splunklib import six
3550

@@ -190,7 +205,7 @@ def test_generatehello_as_unit(self):
190205
self.assertEqual(0, exit_status, msg=six.text_type(errors))
191206
self.assertEqual('', errors)
192207
self._compare_chunks(expected, output, time_sensitive=False)
193-
208+
194209
return
195210

196211
@skipUnless(pypy(), 'Skipping TestSearchCommandsApp.test_pypygeneratetext_as_unit because pypy is not on PATH.')
@@ -426,7 +441,7 @@ def _run_command(self, name, action=None, phase=None, protocol=2):
426441

427442
return expected, output, errors, process.returncode
428443

429-
_Chunk = namedtuple('Chunk', (b'metadata', b'body'))
444+
_Chunk = namedtuple('Chunk', 'metadata body')
430445

431446

432447
if __name__ == "__main__":

tests/test_examples.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
import io
2424

25-
import unittest2
25+
try:
26+
import unittest
27+
except ImportError:
28+
import unittest2 as unittest
2629

2730
from tests import testlib
2831

@@ -32,9 +35,9 @@
3235

3336
def check_multiline(testcase, first, second, message=None):
3437
"""Assert that two multi-line strings are equal."""
35-
testcase.assertTrue(isinstance(first, six.string_types),
38+
testcase.assertTrue(isinstance(first, six.string_types),
3639
'First argument is not a string')
37-
testcase.assertTrue(isinstance(second, six.string_types),
40+
testcase.assertTrue(isinstance(second, six.string_types),
3841
'Second argument is not a string')
3942
# Unix-ize Windows EOL
4043
first = first.replace("\r", "")
@@ -43,7 +46,7 @@ def check_multiline(testcase, first, second, message=None):
4346
testcase.fail("Multiline strings are not equal: %s" % message)
4447

4548

46-
# Run the given python script and return its exit code.
49+
# Run the given python script and return its exit code.
4750
def run(script, stdin=None, stdout=PIPE, stderr=None):
4851
process = start(script, stdin, stdout, stderr)
4952
process.communicate()
@@ -74,7 +77,7 @@ def setUp(self):
7477
# Ignore result, it might already exist
7578
run("index.py create sdk-tests")
7679

77-
@unittest2.skipIf(six.PY3, "Async needs work to support Python 3")
80+
@unittest.skipIf(six.PY3, "Async needs work to support Python 3")
7881
def test_async(self):
7982
result = run("async/async.py sync")
8083
self.assertEquals(result, 0)
@@ -121,12 +124,12 @@ def test_event_types(self):
121124
self.check_commands(
122125
"event_types.py --help",
123126
"event_types.py")
124-
127+
125128
def test_fired_alerts(self):
126129
self.check_commands(
127130
"fired_alerts.py --help",
128131
"fired_alerts.py")
129-
132+
130133
def test_follow(self):
131134
self.check_commands("follow.py --help")
132135

@@ -140,7 +143,7 @@ def test_handlers(self):
140143

141144
# Run the cert handler example with a bad cert file, should error.
142145
result = run(
143-
"handlers/handlers_certs.py --ca_file=handlers/cacert.bad.pem",
146+
"handlers/handlers_certs.py --ca_file=handlers/cacert.bad.pem",
144147
stderr=PIPE)
145148
self.assertNotEquals(result, 0)
146149

@@ -185,7 +188,7 @@ def test_inputs(self):
185188
self.check_commands(
186189
"inputs.py --help",
187190
"inputs.py")
188-
191+
189192
def test_job(self):
190193
self.check_commands(
191194
"job.py --help",
@@ -197,7 +200,7 @@ def test_kvstore(self):
197200
self.check_commands(
198201
"kvstore.py --help",
199202
"kvstore.py")
200-
203+
201204
def test_loggers(self):
202205
self.check_commands(
203206
"loggers.py --help",
@@ -210,7 +213,7 @@ def test_saved_searches(self):
210213
self.check_commands(
211214
"saved_searches.py --help",
212215
"saved_searches.py")
213-
216+
214217
def test_saved_search(self):
215218
temp_name = testlib.tmpname()
216219
self.check_commands(
@@ -227,7 +230,7 @@ def test_search(self):
227230
self.check_commands(
228231
"search.py --help",
229232
["search.py", "search * | head 10"],
230-
["search.py",
233+
["search.py",
231234
"search * | head 10 | stats count", '--output_mode=csv'])
232235

233236
def test_spcmd(self):
@@ -271,7 +274,7 @@ def test_analytics(self):
271274
# Before we start, we'll clean the index
272275
index = service.indexes["sdk-test"]
273276
index.clean()
274-
277+
275278
tracker.track("test_event", distinct_id="abc123", foo="bar", abc="123")
276279
tracker.track("test_event", distinct_id="123abc", abc="12345")
277280

@@ -280,8 +283,8 @@ def test_analytics(self):
280283

281284
# Now, we create a retriever to retrieve the events
282285
retriever = analytics.output.AnalyticsRetriever(
283-
"sdk-test", self.opts.kwargs, index = "sdk-test")
284-
286+
"sdk-test", self.opts.kwargs, index = "sdk-test")
287+
285288
# Assert applications
286289
applications = retriever.applications()
287290
self.assertEquals(len(applications), 1)
@@ -319,17 +322,17 @@ def test_analytics(self):
319322
count = value["count"]
320323
self.assertTrue(name in list(expected_property_values.keys()))
321324
self.assertEqual(count, expected_property_values[name])
322-
325+
323326
# Assert event over time
324327
over_time = retriever.events_over_time(
325328
time_range = analytics.output.TimeRange.MONTH)
326329
self.assertEquals(len(over_time), 1)
327330
self.assertEquals(len(over_time["test_event"]), 1)
328331
self.assertEquals(over_time["test_event"][0]["count"], 2)
329332

330-
# Now that we're done, we'll clean the index
333+
# Now that we're done, we'll clean the index
331334
index.clean()
332-
335+
333336
if __name__ == "__main__":
334337
os.chdir("../examples")
335338
try:

tox.ini

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[tox]
2+
envlist = py27,py35,py36,py37,py38
3+
skipsdist = {env:TOXBUILD:false}
4+
5+
[testenv:pep8]
6+
deps = flake8
7+
flake8-import-order
8+
flake8-blind-except
9+
flake8-builtins
10+
flake8-docstrings
11+
flake8-rst-docstrings
12+
flake8-logging-format
13+
six
14+
commands = flake8
15+
16+
[flake8]
17+
exclude = .tox
18+
# If you need to ignore some error codes in the whole source code
19+
# you can write them here
20+
# ignore = D100,D101
21+
show-source = true
22+
enable-extensions=G
23+
application-import-names = splunk-sdk-python
24+
25+
[testenv]
26+
passenv = LANG
27+
setenv = SPLUNK_HOME=/opt/splunk
28+
INPUT_EXAMPLE_UPLOAD=/opt/splunk/var/log/splunk/splunkd_ui_access.log
29+
whitelist_externals = true
30+
deps = pytest
31+
xmlrunner
32+
unittest2
33+
unittest-xml-reporting
34+
35+
distdir = build
36+
#changedir = tests
37+
#commands_pre = python setup.py dist build
38+
commands =
39+
{env:TOXBUILD:python -m pytest --junitxml=../test-reports/junit-{envname}.xml}

0 commit comments

Comments
 (0)