Skip to content

Commit f0ebc12

Browse files
authored
Switch to Pytest as default runner
1 parent 4a65a54 commit f0ebc12

File tree

12 files changed

+272
-233
lines changed

12 files changed

+272
-233
lines changed

dev-requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pytest
33
pytest-cov
44
coverage
55
mock
6-
nosexcover
76
sphinx<1.7
87
sphinx_rtd_theme
98
jinja2

elasticsearch/helpers/test.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ def _get_client():
4848
return get_test_client()
4949

5050
@classmethod
51-
def setUpClass(cls):
52-
super(ElasticsearchTestCase, cls).setUpClass()
51+
def setup_class(cls):
5352
cls.client = cls._get_client()
5453

55-
def tearDown(self):
56-
super(ElasticsearchTestCase, self).tearDown()
54+
def teardown_method(self, _):
5755
# Hidden indices expanded in wildcards in ES 7.7
5856
expand_wildcards = ["open", "closed"]
59-
if self.es_version >= (7, 7):
57+
if self.es_version() >= (7, 7):
6058
expand_wildcards.append("hidden")
6159

6260
self.client.indices.delete(
@@ -65,7 +63,6 @@ def tearDown(self):
6563
self.client.indices.delete_template(name="*", ignore=404)
6664
self.client.indices.delete_index_template(name="*", ignore=404)
6765

68-
@property
6966
def es_version(self):
7067
if not hasattr(self, "_es_version"):
7168
version_string = self.client.info()["version"]["number"]

setup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
with open(join(dirname(__file__), "README")) as f:
1414
long_description = f.read().strip()
1515

16-
install_requires = ["urllib3>=1.21.1", "certifi"]
16+
install_requires = [
17+
"urllib3>=1.21.1",
18+
"certifi",
19+
]
1720
tests_require = [
1821
"requests>=2.0.0, <3.0.0",
19-
"nose",
2022
"coverage",
2123
"mock",
2224
"pyyaml",
23-
"nosexcover",
25+
"pytest",
26+
"pytest-cov",
2427
]
2528

2629
docs_require = ["sphinx<1.7", "sphinx_rtd_theme"]

test_elasticsearch/README.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ To simply run the tests just execute the ``run_tests.py`` script or invoke
3434

3535
Alternatively, if you wish to control what you are doing you have several additional options:
3636

37-
* ``run_tests.py`` will pass any parameters specified to ``nosetests``
38-
39-
* you can just run your favorite runner in the ``test_elasticsearch`` directory
40-
(verified to work with nose and py.test) and bypass the fetch logic entirely.
37+
* ``run_tests.py`` will pass any parameters specified to ``pytest``
4138

4239
* to run a specific test, you can use ``python3 setup.py test -s <test_name>``, for example
4340
``python3 setup.py test -s test_elasticsearch.test_helpers.TestParallelBulk.test_all_chunks_sent``

test_elasticsearch/test_client/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class TestQueryParams(TestCase):
15-
def setUp(self):
15+
def setup_method(self, _):
1616
self.calls = []
1717

1818
@query_params("simple_param")

test_elasticsearch/test_helpers.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import mock
77
import time
88
import threading
9-
from nose.plugins.skip import SkipTest
9+
import pytest
1010
from elasticsearch import helpers, Elasticsearch
1111
from elasticsearch.serializer import JSONSerializer
1212

@@ -41,7 +41,7 @@ def test_all_chunks_sent(self, _process_bulk_chunk):
4141

4242
self.assertEqual(50, mock_process_bulk_chunk.call_count)
4343

44-
@SkipTest
44+
@pytest.mark.skip
4545
@mock.patch(
4646
"elasticsearch.helpers.actions._process_bulk_chunk",
4747
# make sure we spend some time in the thread
@@ -60,8 +60,7 @@ def test_chunk_sent_from_different_threads(self, _process_bulk_chunk):
6060

6161

6262
class TestChunkActions(TestCase):
63-
def setUp(self):
64-
super(TestChunkActions, self).setUp()
63+
def setup_method(self, _):
6564
self.actions = [({"index": {}}, {"some": u"datá", "i": i}) for i in range(100)]
6665

6766
def test_chunks_are_chopped_by_byte_size(self):

test_elasticsearch/test_serializer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ def test_raises_serialization_error_on_dump_error(self):
144144

145145

146146
class TestDeserializer(TestCase):
147-
def setUp(self):
148-
super(TestDeserializer, self).setUp()
147+
def setup_method(self, _):
149148
self.de = Deserializer(DEFAULT_SERIALIZERS)
150149

151150
def test_deserializes_json_by_default(self):

test_elasticsearch/test_server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_client(**kwargs):
3535
return new_client
3636

3737

38-
def setup():
38+
def setup_module():
3939
get_client()
4040

4141

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Licensed to Elasticsearch B.V under one or more agreements.
2+
# Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information
4+
5+
import os
6+
import time
7+
import pytest
8+
import elasticsearch
9+
10+
11+
@pytest.fixture(scope="function")
12+
def sync_client():
13+
client = None
14+
try:
15+
kw = {
16+
"timeout": 30,
17+
"ca_certs": ".ci/certs/ca.pem",
18+
"connection_class": getattr(
19+
elasticsearch,
20+
os.environ.get("PYTHON_CONNECTION_CLASS", "Urllib3HttpConnection"),
21+
),
22+
}
23+
24+
client = elasticsearch.Elasticsearch(
25+
[os.environ.get("ELASTICSEARCH_HOST", {})], **kw
26+
)
27+
28+
# wait for yellow status
29+
for _ in range(100):
30+
try:
31+
client.cluster.health(wait_for_status="yellow")
32+
break
33+
except ConnectionError:
34+
time.sleep(0.1)
35+
else:
36+
# timeout
37+
pytest.skip("Elasticsearch failed to start.")
38+
39+
yield client
40+
41+
finally:
42+
if client:
43+
version = tuple(
44+
[
45+
int(x) if x.isdigit() else 999
46+
for x in (client.info())["version"]["number"].split(".")
47+
]
48+
)
49+
50+
expand_wildcards = ["open", "closed"]
51+
if version >= (7, 7):
52+
expand_wildcards.append("hidden")
53+
54+
client.indices.delete(
55+
index="*", ignore=404, expand_wildcards=expand_wildcards
56+
)
57+
client.indices.delete_template(name="*", ignore=404)
58+
client.indices.delete_index_template(name="*", ignore=404)
59+
client.transport.close()

test_elasticsearch/test_server/test_helpers.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_all_errors_from_chunk_are_raised_on_failure(self):
7070
assert False, "exception should have been raised"
7171

7272
def test_different_op_types(self):
73-
if self.es_version < (0, 90, 1):
73+
if self.es_version() < (0, 90, 1):
7474
raise SkipTest("update supported since 0.90.1")
7575
self.client.index(index="i", id=45, body={})
7676
self.client.index(index="i", id=42, body={})
@@ -317,10 +317,9 @@ class TestScan(ElasticsearchTestCase):
317317
},
318318
]
319319

320-
@classmethod
321-
def tearDownClass(cls):
322-
cls.client.transport.perform_request("DELETE", "/_search/scroll/_all")
323-
super(TestScan, cls).tearDownClass()
320+
def teardown_method(self, m):
321+
self.client.transport.perform_request("DELETE", "/_search/scroll/_all")
322+
super(TestScan, self).teardown_method(m)
324323

325324
def test_order_can_be_preserved(self):
326325
bulk = []
@@ -490,8 +489,7 @@ def test_clear_scroll(self):
490489

491490

492491
class TestReindex(ElasticsearchTestCase):
493-
def setUp(self):
494-
super(TestReindex, self).setUp()
492+
def setup_method(self, _):
495493
bulk = []
496494
for x in range(100):
497495
bulk.append({"index": {"_index": "test_index", "_id": x}})
@@ -561,8 +559,7 @@ def test_all_documents_get_moved(self):
561559

562560

563561
class TestParentChildReindex(ElasticsearchTestCase):
564-
def setUp(self):
565-
super(TestParentChildReindex, self).setUp()
562+
def setup_method(self, _):
566563
body = {
567564
"settings": {"number_of_shards": 1, "number_of_replicas": 0},
568565
"mappings": {

0 commit comments

Comments
 (0)