Pytest
- pytest ipykernel -vv -s --cov ipykernel --cov-branch --cov-report term-missing:skip-covered --durations 10
Hi
h1-conftest.py
#
#
#
from act.actions import ACT_CONFIG, TOP_PROJECT, TOP_TST, TOP_EXP
#
# toolbox
#
import sys
sys.path.insert(0,ACT_CONFIG['tool_dir'])
# from pprint import pprint
# pprint(sys.path)
#
# https://pawamoy.github.io/posts/save-pytest-logs-as-artifact-gitlab-ci/
# https://stackoverflow.com/questions/41400722/pytest-implementing-a-logfile-per-test-method
##################
import pytest
import pathlib
import datetime
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_setup(item):
# organization by splitting the name with underscore
name_raw = str(item._request.node.name)
name_list = name_raw.split("_")
name4cat = name_list[1].lower() if len(name_list) > 1 else "" #category
name4dir = name_list[2].lower() if len(name_list) > 2 else "" #directory
name4act = name_list[3].lower() if len(name_list) > 3 else "" #action
name4tst = "_".join(name_list[4:]) if len(name_list) > 4 else ""
name4tst = name4tst.lower()
# full name
filename=pathlib.Path('..','..','..',TOP_PROJECT+"_",TOP_TST,TOP_EXP,
name4cat,name4dir,name4act,name4tst,
f"{datetime.datetime.now().strftime('%Y%m%dT_%H%M%S')}.{name_raw}.log"
)
config = item.config
logging_plugin = config.pluginmanager.get_plugin('logging-plugin')
logging_plugin.set_log_path(str(filename))
yield
@pytest.fixture(scope="function")
def my_config(request):
"""
now is the current time
pytest test name
:param request:
:return:
"""
# ds1
my_dict = {'now':f"{datetime.datetime.now().strftime('%Y%m%dT_%H%M%S')}",
'test_name': str(request.function.__name__)
}
my_dict.update(ACT_CONFIG)
return my_dict
h2-conftest
#
#
#
from act.actions import ACT_CONFIG
#
# toolbox
#
import sys
import os
import pytest
import pathlib
import datetime
import pprint as pp
sys.path.insert(0,ACT_CONFIG['tool_dir'])
pp.pprint(sys.path)
#
# https://pawamoy.github.io/posts/save-pytest-logs-as-artifact-gitlab-ci/
# https://stackoverflow.com/questions/41400722/pytest-implementing-a-logfile-per-test-method
##################
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_setup(item):
# organization by splitting the name with underscore
name_raw = str(item._request.node.name)
name_list = name_raw.split("_")
name4cat = name_list[1].lower() if len(name_list) > 1 else "" # category
name4dir = name_list[2].lower() if len(name_list) > 2 else "" # directory
name4act = name_list[3].lower() if len(name_list) > 3 else "" # action
name4tst = "_".join(name_list[4:]) if len(name_list) > 4 else ""
name4tst = name4tst.lower()
# full name
filename = pathlib.Path(ACT_CONFIG['log_dir'],
name4cat, name4dir, name4act, name4tst,
f"{datetime.datetime.now().strftime('%Y%m%dT_%H%M%S')}.{name_raw}.log"
)
pp.pprint(filename)
config = item.config
logging_plugin = config.pluginmanager.get_plugin('logging-plugin')
logging_plugin.set_log_path(str(filename))
yield
@pytest.fixture(scope="function")
def my_config(request):
"""
now is the current time
pytest test name
:param request:
:return:
"""
# ds1
my_dict = {'time_now': f"{datetime.datetime.now().strftime('%Y%m%dT_%H%M%S')}",
'test_name': str(request.function.__name__)
}
my_dict.update(ACT_CONFIG)
#
# [] environment variables
#
os.environ['JAVA_HOME'] = my_dict['JAVA_HOME']
os.environ['GRAPH_ROOT'] = my_dict['tool_dir']
return my_dict
h2-action
#
# [] Constants
#
import os
import pathlib
#
# [] Constants
#
TOP_WORK = pathlib.Path(__file__).parents[1].absolute().__str__()
TOP_SPACE = pathlib.Path(__file__).resolve().parents[5].absolute().__str__()
TOP_PROJECT, TOP_TST, TOP_LOT, TOP_EXP = "odiousgraph", "test2", "hi", "h2"
TOP_LOG, TOP_OUT, TOP_DAT = "log", "out", "dat"
ACT_CONFIG = {
'top_work': TOP_WORK,
'top_space': TOP_SPACE,
'main_dir': str(os.path.abspath(os.path.join(TOP_SPACE, TOP_PROJECT))),
'dat_dir': str(os.path.abspath(os.path.join(TOP_SPACE, TOP_PROJECT + "_", TOP_DAT, TOP_EXP))),
'log_dir': str(os.path.abspath(os.path.join(TOP_SPACE, TOP_PROJECT + "-", TOP_TST, TOP_LOT, TOP_EXP))),
'out_dir': str(os.path.abspath(os.path.join(TOP_SPACE, TOP_PROJECT + "_", TOP_OUT, TOP_LOT, TOP_EXP))),
'tool_dir': os.path.abspath(os.path.join(TOP_SPACE, TOP_PROJECT, 'og', 'ds1')),
'JAVA_HOME': os.getenv('JAVA_HOME')
}
#
#
#
import pprint
pp = pprint.PrettyPrinter(indent=4)
def act_info():
pp.pprint(ACT_CONFIG)
print(" top_work : {} ".format(TOP_WORK))
print(" top_space : {} ".format( TOP_SPACE))
#
#
#
def act_init():
# OUT Directory
for tmp_name in ['out_dir', 'log_dir']:
tmp_dir = ACT_CONFIG[tmp_name]
#
#
#
import shutil
def act_reset():
# OUT Directory
for tmp_name in ['out_dir', 'log_dir']:
tmp_dir = ACT_CONFIG[tmp_name]
print( tmp_name + " : "+ tmp_dir)
if not os.path.exists(tmp_dir):
continue
print( tmp_name + " (before) : " + str(os.listdir(tmp_dir) ))
shutil.rmtree( tmp_dir )
os.mkdir( tmp_dir )
print( tmp_name + " (after) : " + str(os.listdir(tmp_dir) ))
print("\n")
#
#
#
match_act1 = {
'info': act_info,
'reset': act_reset,
'init': act_init
}
#
#
#
import sys
if __name__ == "__main__":
pp.pprint("act-action")
if len(sys.argv) == 1:
pp.pprint( match_act1.keys() )
elif len(sys.argv) > 1:
tmp1 = sys.argv[1]
match_act1[tmp1]()
Hi-Logging
[pytest]
#
# logging
#
log_cli=true
log_cli_level = CRITICAL
log_cli_format = %(message)s
log_file_level = INFO
log_file_format = %(asctime)s [%(levelname)8s] (%(filename)s:%(lineno)s) %(message)s
log_file_date_format=%Y-%m-%d %H:%M:%S
#
# [] directories
#
#testpaths = ch3
#testpaths = util
testpaths = tst
#testpaths = t/oracle
#
# [] include files
#
#python_files = util/test_util_my_design.py
#
# [] exclude certain files
#
#addopts = --ignore=b_hw.py
# [] no logging
#addopts = -p no:logging
#
# [] functions
#
#testpaths = a
#python_files = actions.py
#python_functions = test_*
Hi-Case1
#
#
#
import pytest
import logging
LOG = logging.getLogger(__name__)
#
def test_hi_bare_b11_help(my_config):
LOG.info(" my_config ")
LOG.info( my_config)
Hi - Case2
import pytest
import logging
log = logging.getLogger(__name__)
# @pytest.mark.skip()
class Test_hello_world_core:
@pytest.mark.order(11)
def test_hello_world_core11_help(self, my_config):
log.info(" my_config ")
log.info(my_config)
@pytest.mark.order(12)
@pytest.mark.parametrize("num,output", [('a', 'bcd'), ('e', 'fgh')])
def test_hello_world_core11_param(self, num, output):
log.info(" my_config ")
log.info(num)
log.info(output)
#
#
#
@pytest.mark.skip()
@pytest.mark.order(after="Test_hello_world_core")
class Test_hello_world_export:
@pytest.mark.order(21)
def test_hello_world_export21_help(self, my_config):
my_argv = []
my_argv.append("--config")
my_argv.append(str(my_config["main_dir"]))
my_argv.append("--workflow")
my_argv.append("create_and_export")
log.info(my_argv)
@pytest.mark.skip()
@pytest.mark.order(after="Test_hello_world_export")
class Test_hello_world_import:
@pytest.mark.order(31)
def test_hello_world_import31_help(self, my_config):
log.info(my_config)
Coverage
Snippet
[run]
source
coverage run -m pytest
coverage report -m
coverage html
Sphinx Documentation