Skip to content

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
andgineer committed Apr 15, 2019
1 parent 72526e2 commit 5ebd8ca
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
install:
- python3 -m pip install -r requirements.txt
script:
- python3 -m unittest discover --start-directory bombard --verbose
- python3 -m unittest discover --start-directory tests --verbose
- rm -rf bombard/__pycache__
- python3 -m doctest -v bombard/*.py
- python3 -m doctest -v bombard/*.py tests/*.py
after_success:
- coveralls
14 changes: 0 additions & 14 deletions bombard/tests/fake_args.py

This file was deleted.

14 changes: 0 additions & 14 deletions bombard/tests/stdout_contextmanager.py

This file was deleted.

24 changes: 0 additions & 24 deletions bombard/tests/test_show_folder.py

This file was deleted.

4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ if [[ -z "$@" ]]; then # if selected specific test we do not run doctests
echo
echo "##### Doc tests #####"
rm -rf bombard/__pycache__
python3 -m doctest -v bombard/attr_dict.py bombard/pretty_ns.py
python3 -m doctest -v bombard/attr_dict.py bombard/pretty_ns.py tests/fake_args.py tests/stdout_capture.py
fi
echo
echo "##### Unittest tests #####"
python3.7 -m unittest discover --start-directory bombard --verbose $@
python3.7 -m unittest discover --start-directory tests --verbose $@
1 change: 0 additions & 1 deletion bombard/tests/__init__.py → tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"""
Unbelievable but we need that file for unittest discover to work.
Even python3 does not need that.
"""
25 changes: 25 additions & 0 deletions tests/fake_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Fake args that we can pass to bombard to test it.
This is AttrDict so you can use something like that
>>> args = FakeArgs(init=True)
>>> args.init
True
"""
from bombard.attr_dict import AttrDict
from bombard.args import CAMPAIGN_FILE_NAME, THRESHOLD, TIMEOUT, THREADS_NUM


class FakeArgs(AttrDict):
threads = THREADS_NUM
timeout = TIMEOUT
ms = False
threshold = THRESHOLD
quiet = False
dry = False
verbose = False
quiet = False
log = None
example = None
init = False
examples = False
file_name = CAMPAIGN_FILE_NAME
49 changes: 49 additions & 0 deletions tests/stdout_capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Capture stdout and stderr.
Since Python 3.4 they have (redirect_stdout, redirect_stderr) in contextlib
but my context manager is simpler to use.
Usage:
>>> with CaptureOutput() as captured:
... print('3')
>>> captured.output
'3'
"""
import sys
from io import StringIO


class CaptureOutput:
def __init__(self, capture=True):
""" To see output you can set capture to False"""
self.capture = capture

def __enter__(self):
self.old_out, self.old_err = sys.stdout, sys.stderr
if self.capture:
sys.stdout = self.out = StringIO()
sys.stderr = self.err = StringIO()
return self

@property
def stdout(self):
if self.capture:
return self.out.getvalue().strip()
else:
return ''

@property
def stderr(self):
if self.capture:
return self.err.getvalue().strip()
else:
return ''

@property
def output(self):
return self.stdout + self.stderr

def __exit__(self, *args):
if self.capture:
sys.stdout, sys.stderr = self.old_out, self.old_err
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions tests/test_show_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from tests.stdout_capture import CaptureOutput
import unittest
from bombard.main import campaign
from tests.fake_args import FakeArgs
from bombard.args import INIT_EXAMPLE, DIR_DESC_FILE_NAME, CAMPAIGN_FILE_NAME
import os.path
from bombard.show_descr import markdown_for_terminal


def clean_campaign_file():
""" Removes default campaign from folder root """
if os.path.isfile(CAMPAIGN_FILE_NAME):
os.remove(CAMPAIGN_FILE_NAME)


class TestShowFolder(unittest.TestCase):
def setUp(self):
clean_campaign_file()

def tearDown(self):
clean_campaign_file()

def testShowFolder(self):
with CaptureOutput() as captured:
campaign(FakeArgs(examples=True))

self.maxDiff = 1024
with open(f'bombard/examples/{DIR_DESC_FILE_NAME}') as desc:
self.assertIn( # do not check 1st line of output with the folder name
markdown_for_terminal(desc.read()),
captured.output + '\n'
)
File renamed without changes.

0 comments on commit 5ebd8ca

Please sign in to comment.