use pytest (#297)

* use pytest
This commit is contained in:
Chad Smith 2019-10-07 18:17:31 -07:00 committed by GitHub
parent c52b7ee173
commit 739238feb7
8 changed files with 96 additions and 82 deletions

9
.coveragerc Normal file
View file

@ -0,0 +1,9 @@
[report]
exclude_lines =
pragma: no cover
def __repr__
if self.debug:
if settings.DEBUG
raise AssertionError
raise NotImplementedError
if __name__ == .__main__.:

View file

@ -25,6 +25,9 @@ matrix:
- python: '3.7'
env: NOXSESSION="docs"
dist: xenial
- python: '3.8-dev'
env: NOXSESSION="tests-3.8"
dist: xenial
install:
# commands for linux

View file

@ -13,6 +13,7 @@ prune gdbgui/__pycache__
exclude mypy.ini
exclude .eslintrc.json
exclude .coveragerc
exclude .flake8
exclude .prettierrc.js
exclude jest.config.js

View file

@ -13,11 +13,33 @@ lint_dependencies = ["black", "flake8", "mypy", "check-manifest"]
@nox.session(python=python)
def tests(session):
session.install(".")
session.run("python", "-m", "unittest", "discover")
tests = session.posargs or ["tests"]
session.install(".", "pytest", "pytest-cov")
tests = session.posargs or ["tests"]
session.run(
"pytest", "--cov=gdbgui", "--cov-config", ".coveragerc", "--cov-report=", *tests
)
session.run("yarn", "install", external=True)
session.run("yarn", "test", external=True)
session.run("yarn", "build", external=True)
session.notify("cover")
@nox.session
def cover(session):
"""Coverage analysis"""
session.install("coverage")
session.run(
"coverage",
"report",
"--show-missing",
"--omit=gdbgui/SSLify.py",
"--fail-under=30",
)
session.run("coverage", "erase")
@nox.session(python="3.7")
def lint(session):

View file

@ -68,6 +68,7 @@ setup(
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
python_requires=">=3.6",
project_urls={

View file

@ -1,81 +0,0 @@
"""
Unit tests
Run from top level directory: ./tests/test_app.py
See more on testing Flask apps: http://flask.pocoo.org/docs/0.11/testing/
"""
import unittest
from gdbgui import backend
import sys
from flask_socketio import send, SocketIO # type: ignore
PYTHON3 = sys.version_info.major == 3
backend.setup_backend(testing=True)
socketio = backend.socketio
class TestWebsockets(unittest.TestCase):
@classmethod
def setUp(self):
pass
def tearDown(self):
pass
def test_connect(self):
app = backend.app
socketio = SocketIO()
@socketio.on("connect")
def on_connect():
send({"connected": "foo"}, json=True)
socketio.init_app(app, cookie="foo")
client = socketio.test_client(app)
received = client.get_received()
self.assertEqual(len(received), 1)
self.assertEqual(received[0]["args"], {"connected": "foo"})
class Test(unittest.TestCase):
def setUp(self):
"""Built-in to unittest.TestCase"""
self.app = backend.app.test_client()
def tearDown(self):
"""Built-in to unittest.TestCase"""
pass
def test_load_main_page(self):
response = self.app.get("/")
assert response.status_code == 200
data = response.data.decode() if PYTHON3 else response.data
assert "<!DOCTYPE html>" in data
class TestSocketError(unittest.TestCase):
def test_same_port(self):
backend.setup_backend(testing=True)
def main():
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# commented out for now, seems to be a flask_socketio issue
# https://github.com/miguelgrinberg/Flask-SocketIO/issues/405
suite.addTests(loader.loadTestsFromTestCase(TestWebsockets))
suite.addTests(loader.loadTestsFromTestCase(Test))
suite.addTests(loader.loadTestsFromTestCase(TestSocketError))
runner = unittest.TextTestRunner(verbosity=1)
result = runner.run(suite)
return len(result.errors) + len(result.failures)
if __name__ == "__main__":
main()

37
tests/test_backend.py Executable file
View file

@ -0,0 +1,37 @@
from gdbgui import backend
from flask_socketio import send, SocketIO # type: ignore
import pytest # type: ignore
backend.setup_backend(testing=True)
socketio = backend.socketio
def test_connect():
app = backend.app
socketio = SocketIO()
@socketio.on("connect")
def on_connect():
send({"connected": "foo"}, json=True)
socketio.init_app(app, cookie="foo")
client = socketio.test_client(app)
received = client.get_received()
assert len(received) == 1
assert received[0]["args"] == {"connected": "foo"}
@pytest.fixture
def test_client():
return backend.app.test_client()
def test_load_main_page(test_client):
response = test_client.get("/")
assert response.status_code == 200
assert "<!DOCTYPE html>" in response.data.decode()
def test_same_port():
backend.setup_backend(testing=True)

22
tests/test_main.py Executable file
View file

@ -0,0 +1,22 @@
import gdbgui
import pytest # type: ignore
import sys
@pytest.mark.parametrize(
"test_argv, init_bin_args, gdb_args",
[
(["gdbgui"], [], []),
(["gdbgui", "--gdb-args", "mybin -myargs"], [], ["mybin", "-myargs"]),
(["gdbgui", "--args", "mybin", "-myargs"], ["mybin", "-myargs"], []),
],
)
def test_argument_parsing(monkeypatch, test_argv, init_bin_args, gdb_args):
def mock_setup_backend(*args, **kwargs):
pass
monkeypatch.setattr(gdbgui.backend, "setup_backend", mock_setup_backend)
monkeypatch.setattr(sys, "argv", test_argv)
gdbgui.backend.main()
assert gdbgui.backend.app.config.get("initial_binary_and_args") == init_bin_args
assert gdbgui.backend.app.config.get("gdb_args") == gdb_args