Skip to content

Switch default test runner to Pytest #1231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pytest
pytest-cov
coverage
mock
nosexcover
sphinx<1.7
sphinx_rtd_theme
jinja2
Expand Down
9 changes: 3 additions & 6 deletions elasticsearch/helpers/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ def _get_client():
return get_test_client()

@classmethod
def setUpClass(cls):
super(ElasticsearchTestCase, cls).setUpClass()
def setup_class(cls):
cls.client = cls._get_client()

def tearDown(self):
super(ElasticsearchTestCase, self).tearDown()
def teardown_method(self, _):
# Hidden indices expanded in wildcards in ES 7.7
expand_wildcards = ["open", "closed"]
if self.es_version >= (7, 7):
if self.es_version() >= (7, 7):
expand_wildcards.append("hidden")

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

@property
def es_version(self):
if not hasattr(self, "_es_version"):
version_string = self.client.info()["version"]["number"]
Expand Down
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
with open(join(dirname(__file__), "README")) as f:
long_description = f.read().strip()

install_requires = ["urllib3>=1.21.1", "certifi"]
install_requires = [
"urllib3>=1.21.1",
"certifi",
]
tests_require = [
"requests>=2.0.0, <3.0.0",
"nose",
"coverage",
"mock",
"pyyaml",
"nosexcover",
"pytest",
"pytest-cov",
]

docs_require = ["sphinx<1.7", "sphinx_rtd_theme"]
Expand Down
5 changes: 1 addition & 4 deletions test_elasticsearch/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ To simply run the tests just execute the ``run_tests.py`` script or invoke

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

* ``run_tests.py`` will pass any parameters specified to ``nosetests``

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

* to run a specific test, you can use ``python3 setup.py test -s <test_name>``, for example
``python3 setup.py test -s test_elasticsearch.test_helpers.TestParallelBulk.test_all_chunks_sent``
Expand Down
2 changes: 1 addition & 1 deletion test_elasticsearch/test_client/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class TestQueryParams(TestCase):
def setUp(self):
def setup_method(self, _):
self.calls = []

@query_params("simple_param")
Expand Down
7 changes: 3 additions & 4 deletions test_elasticsearch/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import mock
import time
import threading
from nose.plugins.skip import SkipTest
import pytest
from elasticsearch import helpers, Elasticsearch
from elasticsearch.serializer import JSONSerializer

Expand Down Expand Up @@ -41,7 +41,7 @@ def test_all_chunks_sent(self, _process_bulk_chunk):

self.assertEqual(50, mock_process_bulk_chunk.call_count)

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


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

def test_chunks_are_chopped_by_byte_size(self):
Expand Down
3 changes: 1 addition & 2 deletions test_elasticsearch/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def test_raises_serialization_error_on_dump_error(self):


class TestDeserializer(TestCase):
def setUp(self):
super(TestDeserializer, self).setUp()
def setup_method(self, _):
self.de = Deserializer(DEFAULT_SERIALIZERS)

def test_deserializes_json_by_default(self):
Expand Down
2 changes: 1 addition & 1 deletion test_elasticsearch/test_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_client(**kwargs):
return new_client


def setup():
def setup_module():
get_client()


Expand Down
59 changes: 59 additions & 0 deletions test_elasticsearch/test_server/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Licensed to Elasticsearch B.V under one or more agreements.
# Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information

import os
import time
import pytest
import elasticsearch


@pytest.fixture(scope="function")
def sync_client():
client = None
try:
kw = {
"timeout": 30,
"ca_certs": ".ci/certs/ca.pem",
"connection_class": getattr(
elasticsearch,
os.environ.get("PYTHON_CONNECTION_CLASS", "Urllib3HttpConnection"),
),
}

client = elasticsearch.Elasticsearch(
[os.environ.get("ELASTICSEARCH_HOST", {})], **kw
)

# wait for yellow status
for _ in range(100):
try:
client.cluster.health(wait_for_status="yellow")
break
except ConnectionError:
time.sleep(0.1)
else:
# timeout
pytest.skip("Elasticsearch failed to start.")

yield client

finally:
if client:
version = tuple(
[
int(x) if x.isdigit() else 999
for x in (client.info())["version"]["number"].split(".")
]
)

expand_wildcards = ["open", "closed"]
if version >= (7, 7):
expand_wildcards.append("hidden")

client.indices.delete(
index="*", ignore=404, expand_wildcards=expand_wildcards
)
client.indices.delete_template(name="*", ignore=404)
client.indices.delete_index_template(name="*", ignore=404)
client.transport.close()
15 changes: 6 additions & 9 deletions test_elasticsearch/test_server/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_all_errors_from_chunk_are_raised_on_failure(self):
assert False, "exception should have been raised"

def test_different_op_types(self):
if self.es_version < (0, 90, 1):
if self.es_version() < (0, 90, 1):
raise SkipTest("update supported since 0.90.1")
self.client.index(index="i", id=45, body={})
self.client.index(index="i", id=42, body={})
Expand Down Expand Up @@ -317,10 +317,9 @@ class TestScan(ElasticsearchTestCase):
},
]

@classmethod
def tearDownClass(cls):
cls.client.transport.perform_request("DELETE", "/_search/scroll/_all")
super(TestScan, cls).tearDownClass()
def teardown_method(self, m):
self.client.transport.perform_request("DELETE", "/_search/scroll/_all")
super(TestScan, self).teardown_method(m)

def test_order_can_be_preserved(self):
bulk = []
Expand Down Expand Up @@ -490,8 +489,7 @@ def test_clear_scroll(self):


class TestReindex(ElasticsearchTestCase):
def setUp(self):
super(TestReindex, self).setUp()
def setup_method(self, _):
bulk = []
for x in range(100):
bulk.append({"index": {"_index": "test_index", "_id": x}})
Expand Down Expand Up @@ -561,8 +559,7 @@ def test_all_documents_get_moved(self):


class TestParentChildReindex(ElasticsearchTestCase):
def setUp(self):
super(TestParentChildReindex, self).setUp()
def setup_method(self, _):
body = {
"settings": {"number_of_shards": 1, "number_of_replicas": 0},
"mappings": {
Expand Down
Loading