Commit 2a755697 authored by rajesh's avatar rajesh

Convert unit testcases into pytest cases

parent a2a797e9
Pipeline #1484 failed with stages
in 29 seconds
......@@ -208,6 +208,7 @@ def help(cb=None):
cb = Callback.factory(cb or context.default_module)
def _modhelp():
moddoc = False
if cb.filename:
moddoc = filedoc(cb.filename)
"""Clitoo Test cases."""
import clitoo
import sys
import os
import shutil
import pytest
class TestClitoo(object):
  • why have a class here ?

  • As pytest says Once you develop multiple tests, you may want to group them into a class. pytest makes it easy to create a class containing more than one test that's why I create the class to cover all cases.

    Edited by rajesh
Please register or sign in to reply
"""Classes can still be used to organize collections of test cases, with.
each test being a Method on the Class, rather than a standalone function.
url = ""
def test_python_3_version_compatibility(self):
"""Test python 3 version comptibility."""
pyvestion = sys.version.split(".")
assert pyvestion[0] == '3' and pyvestion[1] == '6'
def test_python_2_version_compatibility(self):
"""Test python 2 version comptibility."""
pyvestion = sys.version.split(".")
assert pyvestion[0] != '2'
def test_context_valid_args_factory(self):
"""Test aggument and keyword argument for factory."""
argvlist = ['-a', '--b', '--something=to see', 'how', 'it=parses']
ct = clitoo.Context.factory(argvlist)
assert set({'something': 'to see'}).issubset(ct.kwargs)
assert set(['a', 'b']).issubset(ct.args)
def test_context_invalid_single_dash_kwargs_factory(self):
"""Test single dash keyword argument for context factory."""
argvlist = ['-a', '--b', '--something=-', 'how', 'it=-parses']
with pytest.raises(OSError):
def test_context_invalid_double_dash_argument_factory(self):
"""Test aggument and keyword argument for factory."""
argvlist = ['-a', '--b', '--', '--something=to see', 'how', 'it=parses']
with pytest.raises(OSError):
def test_expand_invalid_single_dash_argument_factory(self):
"""Test invalid argument format for the expand."""
argvlist = ['your.func', '-a', '-', '--b', '--something=to see', 'how',
with pytest.raises(OSError):
def test_context_empty_factory(self):
"""Test aggument and keyword argument for factory."""
argvlist = []
ct = clitoo.Context.factory(argvlist)
assert set({}).issubset(ct.kwargs)
assert set([]).issubset(ct.args)
def test_factory(self):
"""Test pass for avaliable callback.
Test nested attribute for factory.
from clitoo.git import clone, push, apply
cb = clitoo.Callback.factory('clitoo.git.clone')
assert cb.cb == clone
cb = clitoo.Callback.factory('clitoo.git.push')
assert cb.cb == push
cb = clitoo.Callback.factory('clitoo.git.apply')
assert cb.cb == apply
def test_factory_fail(self):
"""Test fail for not avaliable callback and nested attribute for factory."""
cb = clitoo.Callback.factory('clitoo.git.reset')
assert cb.cb is None
def test_callback_fail(self):
"""Test fail for not avaliable callback."""
cb = clitoo.Callback.factory('clitooofail')
assert cb.cb is None
def test_callback_callables(self):
"""The test success, If the callback is available."""
cb = clitoo.Callback.factory('')
assert 'help' in cb.callables
assert 'debug' in cb.callables
def test_class_properties(self):
Check class didnt change from a defined snapshot. Class changes or new.
references makes this test fail to warn the dev, who will decide if the.
change must be accounted.
def _check_class_properties(model_class, properties_list_snapshot):
"""Test properties of the class."""
fields = [field for field, _ in model_class.__dict__.items()]
missing_list = set(properties_list_snapshot) - set(fields)
new_list = set(fields) - set(properties_list_snapshot)
assert len(missing_list) == 0 and len(new_list) == 0
callback_props = ['path', 'module', 'modname', 'funcname', 'parts',
'cb', 'callables']
_check_class_properties(clitoo.Callback(), callback_props)
context_props = ['args', 'kwargs', 'argv', 'default_module']
_check_class_properties(clitoo.Context(), context_props)
def test_expand(self):
"""Test format of the argument and keyword argument to expanding."""
argvlist = ['your.func', '-a', '--b', '--something=to see', 'how',
ex_args, ex_kwargs = clitoo.expand(*argvlist[1:])
assert set(ex_args).issubset(['how'])
assert set(ex_kwargs).issubset({'it': 'parses'})
def test_filenamne(self):
"""Test filename of callback function."""
cb = clitoo.Callback.factory('help')
assert len(cb.filename) != 0
def test_no_filedoc(self):
"""Return default doc string.If doc string is not avaliable for followling(clone,push,)."""
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
file_path = base_dir + '/clitoo/clitoo/'
doc_text = clitoo.filedoc(file_path)
assert 'Document me !' == doc_text
class Foo:
bar = {'baz': [lambda *a, **k: 'test']}
def test_main(self):
"""Test main method."""
clitoo.main(['debug', 'git.clone'])
def test_run_cmd(self):
"""Test _run function of the git module."""
base_dir = os.path.dirname(os.path.abspath(__file__))
app_path = base_dir + "/clitoo/testcli"
cmd = ['clone', self.url]
assert os.path.isdir(app_path)
def test_callback_nested_attribute():
cb = clitoo.Callback.factory('')
assert cb.cb ==['baz'][0]
def test_clone_cmd(self):
"""Test clone of the git module."""
base_dir = os.path.dirname(os.path.abspath(__file__))
clitoo.git.clone(self.url, base_dir + "/clitoo/testcli")
assert os.path.isdir(base_dir + "/clitoo/testcli/clitoo")
shutil.rmtree(base_dir + "/clitoo/testcli")
def test_apply_cmd(self):
"""Test apply of the git module."""
def test_callback_nested_module():
from clitoo.git import clone
cb = clitoo.Callback.factory('clitoo.git.clone')
assert cb.cb == clone
def test_apply_help_cmd(self):
"""Return the help doc string for the apply."""'git.apply')
def test_push_help_cmd(self):
"""Return the help doc string for the push."""'git.push')
def test_callback_fail():
cb = clitoo.Callback.factory('clitoooooooolaaaawwwwllllzz')
assert cb.cb is None
def test_reset_help_cmd(self):
"""Return the help doc string for the clone."""
with pytest.raises(AttributeError):
def test_help(self):
"""Test help method."""
def test_callback_callables():
cb = clitoo.Callback.factory('clitoo')
assert 'help' in cb.callables
def test_empty_debug(self):
"""Test debug method."""
def test_args_debug(self):
"""Test debug method."""
clitoo.debug(*('git.clone', '12'))
def test_help(capsys):'clitoo')
def test_not_exists_method(self):
"""Test not avaliable method for clitoo module."""
with pytest.raises(AttributeError):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment