Added integration testing.

This commit is contained in:
2022-06-03 11:52:18 -04:00
parent 015edc13c1
commit a9b1920c2f
11 changed files with 311 additions and 3 deletions

0
tests/__init__.py Normal file
View File

View File

@ -0,0 +1,9 @@
Feature: Server Start
Acceptance testing related to server start up.
Scenario: Default start up
Given a server
And it is running
When the home page is accessed
Then the status should be OK

View File

View File

@ -0,0 +1,17 @@
"""PyTest configuration."""
from pytest import fixture
from .server import Server
from .page import Page
@fixture
def server():
"""Create a server instance."""
serv = Server()
yield serv
serv.destroy()
@fixture
def page():
pg = Page()
return pg

22
tests/step_defs/page.py Normal file
View File

@ -0,0 +1,22 @@
"""Fisture class for page functions."""
from urllib import request
from urllib.error import HTTPError
class Page:
"""Class for getting an individual page."""
def __init__(self):
"""Init function."""
self.res = None
def request_url(self, url):
"""Make a page request."""
try:
self.res = request.urlopen(url)
except HTTPError as err:
self.res = err
def get_status_code(self):
"""Return the status code."""
return self.res.status

40
tests/step_defs/server.py Normal file
View File

@ -0,0 +1,40 @@
"""Fixture for setting up a single server."""
from asyncio import create_subprocess_exec, get_event_loop, sleep
from pathlib import Path
class Server:
"""Setup a single server."""
def __init__(self):
"""Initialization of a server."""
self.settings = {}
self.settings["address"] = "127.0.0.1"
self.settings["port"] = 9090
self.process = None
self.loop = get_event_loop()
async def __start(self):
"""async start of the server."""
self.process = await create_subprocess_exec(
Path.cwd().joinpath("target", "release", "morethantext_web")
)
await sleep(1)
async def __stop(self):
"""async stop of the server."""
if self.process is not None and self.process.returncode is None:
self.process.terminate()
await self.process.wait()
def start(self):
"""Start the server."""
self.loop.run_until_complete(self.__start())
def stop(self):
"""Stop the server."""
self.loop.run_until_complete(self.__stop())
def destroy(self):
"""Removes the server instance."""
self.stop()

View File

@ -0,0 +1,25 @@
"""Interpratures for server start features."""
from pytest_bdd import given, scenarios, then, when
scenarios("../features/server_start.feature")
@given("a server")
def create_server(server):
"""Set up a server."""
@given("it is running")
def start_server(server):
"""Start up the server."""
server.start()
@when("the home page is accessed")
def access_home_page(server, page):
"""Access the home page."""
url = f"http://{server.settings['address']}:{server.settings['port']}/"
page.request_url(url)
@then("the status should be OK")
def check_for_ok(page):
"""Is the page status code 200"""
assert page.get_status_code() == 200