Skip to content

Commit

Permalink
Got rid of BaseTest. Simplified fixtures setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmrz committed Mar 1, 2016
1 parent 8394f47 commit a423dab
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
FLAGS=
SCALA_VERSION?=2.11
KAFKA_VERSION?=0.9.0.1
DOCKER_IMAGE_NAME=pygo/kafka
DOCKER_IMAGE=pygo/kafka:$(SCALA_VERSION)_$(KAFKA_VERSION)

flake:
flake8 aiokafka tests

test: flake
@py.test -s --no-print-logs --scala-version $(SCALA_VERSION) --kafka-version $(KAFKA_VERSION) --docker-image-name $(DOCKER_IMAGE_NAME) $(FLAGS) tests
@py.test -s --no-print-logs --docker-image $(DOCKER_IMAGE) $(FLAGS) tests

vtest: flake
@py.test -s -v --no-print-logs --scala-version $(SCALA_VERSION) --kafka-version $(KAFKA_VERSION) --docker-image-name $(DOCKER_IMAGE_NAME) $(FLAGS) tests
@py.test -s -v --no-print-logs --docker-image $(DOCKER_IMAGE) $(FLAGS) tests

cov cover coverage:
@py.test -s --no-print-logs --cov aiokafka --cov-report html --scala-version $(SCALA_VERSION) --kafka-version $(KAFKA_VERSION) --docker-image-name $(DOCKER_IMAGE_NAME) $(FLAGS) tests
@py.test -s --no-print-logs --cov aiokafka --cov-report html --docker-image $(DOCKER_IMAGE) $(FLAGS) tests
@echo "open file://`pwd`/htmlcov/index.html"

clean:
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Docker is required to run tests. See https://docs.docker.com/engine/installation

Setting up tests requirements (assuming you're within virtualenv on ubuntu 14.04+)::

sudo apt-get install -y libsnappy-dev && pip install flake8 pytest pytest-cov pytest-catchlog docker-py python-snappy coveralls .
sudo apt-get install -y libsnappy-dev
pip install flake8 pytest pytest-cov pytest-catchlog docker-py python-snappy coveralls .

Running tests::

Expand Down
11 changes: 1 addition & 10 deletions tests/_testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ def wrapper(test, *args, **kw):


@pytest.mark.usefixtures('setup_test_class', 'kafka_server')
class BaseTest(unittest.TestCase):
"""Base test case for unittests.
"""

@pytest.fixture(autouse=True)
def setup_kafka_host_and_port(self, kafka_server):
self.kafka_host, self.kafka_port = kafka_server


class KafkaIntegrationTestCase(BaseTest):
class KafkaIntegrationTestCase(unittest.TestCase):

topic = None

Expand Down
26 changes: 8 additions & 18 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@


def pytest_addoption(parser):
parser.addoption('--docker-image-name',
parser.addoption('--docker-image',
action='store',
default='pygo/kafka',
help='Kafka docker image name')
parser.addoption('--kafka-version',
action='store',
default='0.9.0.1',
help='Kafka version')
parser.addoption('--scala-version',
action='store',
default='2.11',
help='Scala version')
default='pygo/kafka:2.11_0.9.0.1',
help='Kafka docker image to use')


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -65,15 +57,12 @@ def session_id():

@pytest.yield_fixture(scope='session')
def kafka_server(request, docker, docker_ip_address, unused_port, session_id):
image_name = request.config.getoption('--docker-image-name')
skala_version = request.config.getoption('--scala-version')
kafka_version = request.config.getoption('--kafka-version')
image_tag = '{}:{}_{}'.format(image_name, skala_version, kafka_version)
docker.pull(image_tag)
image = request.config.getoption('--docker-image')
docker.pull(image)
kafka_host = docker_ip_address
kafka_port = unused_port()
container = docker.create_container(
image=image_tag,
image=image,
name='aiokafka-tests-{}'.format(session_id),
ports=[2181, 9092],
environment={
Expand Down Expand Up @@ -108,5 +97,6 @@ def loop(request):


@pytest.fixture(scope='class')
def setup_test_class(request, loop):
def setup_test_class(request, loop, kafka_server):
request.cls.loop = loop
request.cls.kafka_host, request.cls.kafka_port = kafka_server
15 changes: 5 additions & 10 deletions tests/test_conn.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import asyncio
import unittest
import functools
import pytest
from unittest import mock
from kafka.common import MetadataResponse, ProduceRequest, ConnectionError
from kafka.protocol import KafkaProtocol, create_message_set

from aiokafka.conn import AIOKafkaConnection, create_conn
from ._testutil import BaseTest, run_until_complete
from ._testutil import run_until_complete


@pytest.mark.usefixtures('setup_test_class')
class ConnTest(unittest.TestCase):

def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.conn = AIOKafkaConnection('localhost', 1234, loop=self.loop)

def tearDown(self):
self.loop.close()

def test_ctor(self):
conn = AIOKafkaConnection('localhost', 1234, loop=self.loop)
self.assertEqual('localhost', conn.host)
Expand All @@ -28,7 +22,8 @@ def test_ctor(self):
self.assertIsNone(conn._writer)


class ConnIntegrationTest(BaseTest):
@pytest.mark.usefixtures('setup_test_class')
class ConnIntegrationTest(unittest.TestCase):

@run_until_complete
def test_global_loop_for_create_conn(self):
Expand Down
9 changes: 2 additions & 7 deletions tests/test_producer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import pytest
import uuid
import unittest
from unittest import mock
Expand All @@ -20,15 +21,9 @@
from aiokafka.client import AIOKafkaClient


@pytest.mark.usefixtures('setup_test_class')
class TestKafkaProducer(unittest.TestCase):

def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)

def tearDown(self):
self.loop.close()

def test_invalid_codec(self):
client = mock.Mock()
with self.assertRaises(UnsupportedCodecError):
Expand Down

0 comments on commit a423dab

Please sign in to comment.