Skip to content

Commit db8f08b

Browse files
committed
Switch to pytest for the tests
1 parent 678f305 commit db8f08b

8 files changed

+119
-108
lines changed

.travis.yml

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
os: linux
2+
dist: xenial
13
language: python
2-
34
python:
4-
- "2.7"
5-
- "3.3"
6-
- "3.4"
5+
- "3.7"
6+
- "3.6"
77
- "3.5"
8+
- "3.4"
9+
- "2.7"
10+
env: TOXENV=py
11+
12+
install:
13+
- pip install tox
814

9-
install: "pip install -r requirements.txt"
10-
script: nosetests
15+
script:
16+
- tox

setup.py

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
packages=['whatportis'],
2020
include_package_data=True,
2121
install_requires=requirements,
22+
extras_require={
23+
"dev": [
24+
"pytest",
25+
"tox"
26+
]
27+
},
2228
entry_points=entry_points,
2329
classifiers=(
2430
'Development Status :: 5 - Production/Stable',

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
from click.testing import CliRunner
3+
4+
5+
@pytest.fixture
6+
def runner():
7+
return CliRunner()

tests/test_cli.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import re
2+
3+
from whatportis.__main__ import run
4+
5+
6+
def test_search_with_no_arg(runner):
7+
result = runner.invoke(run, [])
8+
regex = re.compile('^Usage:')
9+
assert result.exit_code == 2
10+
assert regex.match(result.output)
11+
12+
13+
def test_search_with_multiple_args(runner):
14+
result = runner.invoke(run, ['foo', 'bar'])
15+
regex = re.compile('^Usage:')
16+
assert result.exit_code == 2
17+
assert regex.match(result.output)
18+
19+
20+
def test_search_str_port(runner):
21+
result = runner.invoke(run, ['redis'])
22+
assert result.exit_code == 0
23+
assert "redis" in result.output
24+
assert "6379" in result.output
25+
assert "tcp" in result.output
26+
assert "An advanced key-value cache and store" in result.output
27+
28+
29+
def test_search_int_port(runner):
30+
result = runner.invoke(run, ['3306'])
31+
assert result.exit_code == 0
32+
assert result.output.count('mysql') == 1
33+
assert result.output.count('3306') == 1
34+
assert "tcp" in result.output
35+
assert "udp" in result.output
36+
37+
38+
def test_search_str_like_port(runner):
39+
result = runner.invoke(run, ['mysql', '--like'])
40+
assert result.exit_code == 0
41+
assert result.output.count('mysql') == 6
42+
assert result.output.count('tcp') == 6
43+
assert result.output.count('udp') == 5
44+
45+
46+
def test_search_int_like_port(runner):
47+
result = runner.invoke(run, ['3306', '--like'])
48+
assert result.exit_code == 0
49+
assert result.output.count('mysql') == 2
50+
assert result.output.count('tcp') == 2
51+
assert result.output.count('udp') == 2

tests/test_utils.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from whatportis.core import merge_protocols
2+
3+
4+
def test_merge_protocols_different_ports():
5+
ports = [{
6+
"description": "My description 1",
7+
"name": "MyName 1",
8+
"port": "1234",
9+
"protocol": "udp"
10+
}, {
11+
"description": "My description 2",
12+
"name": "MyName 2",
13+
"port": "5678",
14+
"protocol": "tcp"
15+
}]
16+
result = merge_protocols(ports)
17+
assert sorted(result, key=lambda p: p["name"]) == sorted(ports, key=lambda p: p["name"])
18+
19+
20+
def test_merge_protocols_same_port():
21+
ports = [{
22+
"description": "My description",
23+
"name": "MyName",
24+
"port": "1234",
25+
"protocol": "udp"
26+
}, {
27+
"description": "My description",
28+
"name": "MyName",
29+
"port": "1234",
30+
"protocol": "tcp"
31+
}]
32+
result = merge_protocols(ports)
33+
assert result == [{
34+
"description": "My description",
35+
"name": "MyName",
36+
"port": "1234",
37+
"protocol": "udp, tcp"
38+
}]

tests/test_whatportis.py

-98
This file was deleted.

tox.ini

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[tox]
2-
envlist = py27,py33,py34,py35
2+
envlist = py{27,33,34,35,36,37}
33

44
[testenv]
5-
deps=-rrequirements.txt
6-
commands =
7-
python tests/test_whatportis.py
5+
deps=
6+
pytest
7+
-rrequirements.txt
8+
commands = pytest

0 commit comments

Comments
 (0)