Changeset - 6b29d9bd17de
[Not reviewed]
0 1 1
Branko Majic (branko) - 6 years ago 2018-02-27 15:08:36
branko@majic.rs
GC-3: Reduce code duplication in tests by abstracting away command runs in tests.
2 files changed with 59 insertions and 17 deletions:
0 comments (0 inline, 0 general)
functional_tests/base.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 
#
 
# Copyright (C) 2018 Branko Majic
 
#
 
# This file is part of Gimmecert.
 
#
 
# Gimmecert is free software: you can redistribute it and/or modify it
 
# under the terms of the GNU General Public License as published by the Free
 
# Software Foundation, either version 3 of the License, or (at your option) any
 
# later version.
 
#
 
# Gimmecert is distributed in the hope that it will be useful, but
 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
# details.
 
#
 
# You should have received a copy of the GNU General Public License along with
 
# Gimmecert.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 

	
 
import subprocess
 

	
 

	
 
def run_command(command, *args):
 
    """
 
    Helper function that runs the specified command, and takes care of
 
    some tedious work, like converting the stdout and stderr to
 
    correct encoding etc.
 

	
 
    This is essentially a small wrapper around the subprocess.Popen.
 

	
 
    :param command: Command that should be run.
 
    :type command: str
 

	
 
    :param *args: Zero or more arguments to pass to the command.
 
    :type *args: str
 

	
 
    :returns: (stdout, stderr, exit_code) -- Standard output, error, and exit code captured from running the command.
 
    :rtype: (str, str, int)
 
    """
 

	
 
    invocation = [command]
 
    invocation.extend(args)
 

	
 
    process = subprocess.Popen(invocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout, stderr = process.communicate()
 
    stdout, stderr = stdout.decode(), stderr.decode()
 

	
 
    return stdout, stderr, process.returncode
functional_tests/test_help.py
Show inline comments
 
@@ -19,7 +19,7 @@
 
#
 

	
 

	
 
import subprocess
 
from .base import run_command
 

	
 

	
 
def test_cli_works():
 
@@ -34,43 +34,37 @@ def test_cli_works():
 
    # Before moving on, John decides to quickly test if the package
 
    # has been installed correctly. Assuming that the command is the
 
    # same as package name, he runs it.
 
    process = subprocess.Popen(["gimmecert"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout, stderr = process.communicate()
 
    stdout, stderr = stdout.decode(), stderr.decode()
 
    stdout, stderr, returncode = run_command("gimmecert")
 

	
 
    # John has a look at output, and notices that no error has been
 
    # reported. He also verifies the return code is non-zero, just to
 
    # be on the safe side.
 
    assert stderr == ''
 
    assert process.returncode == 0
 
    assert returncode == 0
 

	
 

	
 
def test_usage_help_shown():
 
    # Since John feels a bit lazy, he decides to skip reading the
 
    # documentation, and just run the tool to see if he gets any
 
    # useful help.
 
    process = subprocess.Popen(["gimmecert"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout, stderr = process.communicate()
 
    stdout, stderr = stdout.decode(), stderr.decode()
 
    stdout, stderr, returncode = run_command("gimmecert")
 

	
 
    # John is presented with short usage instructions.
 
    assert "usage: gimmecert [-h]" in stdout
 
    assert stderr == ''
 
    assert process.returncode == 0
 
    assert returncode == 0
 

	
 

	
 
def test_extended_help_shown():
 
    # John is still not quite sure how the tool works. Therefore he
 
    # decides to try out the -h flag to the command.
 
    process = subprocess.Popen(["gimmecert", "-h"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout_h_flag, stderr_h_flag = process.communicate()
 
    stdout_h_flag, stderr_h_flag = stdout_h_flag.decode(), stderr_h_flag.decode()
 
    stdout_h_flag, stderr_h_flag, returncode_h_flag = run_command("gimmecert", "-h")
 

	
 
    # In doing so, John is presented with much more extensive
 
    # instructions that provide him with better idea on how to use the
 
    # tool.
 
    assert stderr_h_flag == ''
 
    assert process.returncode == 0
 
    assert returncode_h_flag == 0
 
    assert "usage: gimmecert [-h]" in stdout_h_flag
 
    assert "Examples:" in stdout_h_flag
 
    assert "optional arguments" in stdout_h_flag
 
@@ -82,12 +76,10 @@ def test_extended_help_shown():
 

	
 
    # John also notices the help command in the list. He tries that
 
    # one out as well.
 
    process = subprocess.Popen(["gimmecert", "help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
    stdout_help, stderr_help = process.communicate()
 
    stdout_help, stderr_help = stdout_help.decode(), stderr_help.decode()
 
    stdout_help, stderr_help, returncode_help = run_command("gimmecert", "help")
 

	
 
    # John is presented with identical results as when running just
 
    # with the -h flag.
 
    assert process.returncode == 0
 
    assert returncode_help == 0
 
    assert stdout_help == stdout_h_flag
 
    assert stderr_help == stderr_h_flag
0 comments (0 inline, 0 general)