在我的文档中包含外部测试结果

在我的文档中包含外部测试结果

我有一个 LaTeX 文档,其中包含我用来布局软件系统集成测试规范的环境。我想做的一件事是将上次运行的结果(如果有)以图章或水印的形式合并到文档中,这样就可以清楚地知道测试是通过还是失败。

我目前使用todo环境来实现这一点,因为它还为我在“目录”中提供了一个颜色鲜艳的方块。目前我有以下内容(这与实际文档相比非常简化,但确实说明了我的问题):

\documentclass[11pt, a4paper]{article}

\usepackage{booktabs}
\usepackage{framed}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{array}
\usepackage[colorinlistoftodos,prependcaption,textsize=tiny]{todonotes}
\usepackage{tikz}
\usepackage{xstring}
\usepackage[utf8]{inputenc}
\usepackage{datatool,xparse}


% This command basically takes a text snippet and removes all dashes and replaces all numbers by a word. This way, the
% code of a scenario can be made into a command
\NewDocumentCommand{\removeDashesAndNumbers}{m}{%
  \saveexpandmode\noexpandarg
  \def\tempstring{#1}%
  \xStrSubstitute{\tempstring}{1}{one}[\tempstring]%
  \xStrSubstitute{\tempstring}{2}{two}[\tempstring]%
  \xStrSubstitute{\tempstring}{3}{three}[\tempstring]%
  \xStrSubstitute{\tempstring}{4}{four}[\tempstring]%
  \xStrSubstitute{\tempstring}{5}{five}[\tempstring]%
  \xStrSubstitute{\tempstring}{6}{six}[\tempstring]%
  \xStrSubstitute{\tempstring}{7}{seven}[\tempstring]%
  \xStrSubstitute{\tempstring}{8}{eight}[\tempstring]%
  \xStrSubstitute{\tempstring}{9}{nine}[\tempstring]%
  \xStrSubstitute{\tempstring}{0}{zero}[\tempstring]%                           
  \xStrSubstitute{\tempstring}{-}{}[\tempstring]%                               
  \tempstring
  \restoreexpandmode
}
\newcommand*{\xStrSubstitute}{%
  \expandafter\StrSubstitute\expandafter
}


\tikzstyle{inlinenotestyle} = [
    text width=0 pt
        draw=white
        fill=white
        line width=0 pt
        ]

\hfuzz=6pt
\vfuzz=6pt
\sloppy
\parindent 0pt
\parskip 10pt

\newcommand\longempty{}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcommand{\code}[1]{\texttt{\expandafter\detokenize\expandafter{#1}}}
\newcommand{\caseref}[1]{\hyperref[case:#1]{#1} on page \pageref{case:#1}}

\newcommand{\testsuccess}[1]{
\todo[noline, inline, color=green, size=\large, noprepend, caption={#1: Success}]
{\begin{minipage}{\linewidth}
     \hspace{-3.2cm} \includegraphics[width=3cm]{PassStamp.png}
    \vspace{-3.5cm}
 \end{minipage}
}
}

\newcommand{\testfailure}[1]{
\todo[noline, inline, color=red, size=\large, noprepend, caption={#1: Failed}]
{\begin{minipage}{\linewidth}
     \hspace{-3.2cm} \includegraphics[width=3cm]{FailStamp.png}
    \vspace{-3.5cm}
 \end{minipage}
}
}

\newcommand{\testnotrun}[1]{
\todo[noline, inline, color=grey, size=\large, noprepend, caption={#1: Not executed}]
{\begin{minipage}{\linewidth}
     \hspace{-3.2cm} \includegraphics[width=3cm]{SkipStamp.png}
    \vspace{-3.5cm}
 \end{minipage}
}
}

% -------------------------------------------------------
% New environment for testcases
% -------------------------------------------------------
\newenvironment{testcase}
{
\newcommand{\caseid}[1]{
    \renewcommand{\givenid}{##1}\label{case:##1}
}
\newcommand{\casetitle}[1]{\renewcommand{\giventitle}{##1}}
\newcommand{\casedescription}[1]{\renewcommand{\givendescription}{##1}}
\newcommand{\casereason}[1]{\renewcommand{\givenreason}{##1}}
\newcommand{\caseinput}[1]{\renewcommand{\givencaseinput}{##1}}
\newcommand{\caseresult}[1]{\renewcommand{\givencaseresult}{##1}}
\newcommand{\caseapis}[1]{\renewcommand{\givencaseapis}{##1}}
\newcommand{\casesteps}[1]{\renewcommand{\givencasesteps}{##1}}
\newcommand{\caseremark}[1]{\renewcommand{\givencaseremark}{##1}}
\newcommand{\givenid}{Required!}
\newcommand{\giventitle}{Required!}
\newcommand{\givendescription}{Required!}
\newcommand{\givenreason}{}
\newcommand{\givencaseremark}{}
\newcommand{\givencaseinput}{}
\newcommand{\givencaseapis}{}
\newcommand{\givencaseresult}{Required!}
\newcommand{\givencasesteps}{Required!} 
}
{
\begin{framed}  
\textbf{\givenid \ : \giventitle }
\end{framed}
\if\relax\givencaseremark\relax
\else
    \todo[noline, color=green]{\givencaseremark}
\fi
\addcontentsline{toc}{subsubsection}{\givenid \ : \giventitle }
\if\relax\givendescription\relax
\else
    \begin{tabular}{p{4cm}p{10cm} }
  \textbf{Description:} & \givendescription
    \end{tabular}
\fi

\ifx\givenreason\longempty
\else
    \begin{tabular}{p{4cm}p{10cm} }
  \textbf{Rationale:} & \givenreason
    \end{tabular}
\fi

\ifx\givencaseinput\longempty
\else
    \begin{tabular}{p{14cm}}
  \textbf{Needed data:} \\
    \end{tabular}

  \begin{enumerate}
        \givencaseinput 
    \end{enumerate}
\fi

\ifx\givencaseapis\longempty
\else
    \begin{tabular}{p{4cm}p{10cm} }
  \textbf{Used APIs:} & \ \\
    \end{tabular}

  \begin{enumerate}
        \givencaseapis 
    \end{enumerate}
\fi

\ifx\givencasesteps\longempty
\else
    \begin{tabular}{p{4cm}p{10cm} }
  \textbf{Steps:} & \ \\
    \end{tabular}

  \begin{enumerate}
        \givencasesteps  
    \end{enumerate}
\fi

\ifx\givencaseresult\longempty
\else
    \begin{tabular}{p{4cm}p{10cm} }
  \textbf{Expected result:} & \ \\
    \end{tabular}

  \begin{enumerate}
        \givencaseresult  
    \end{enumerate} 
\fi

\newpage
}


\begin{document}

\listoftodos[Testresults]

\newpage

\testsuccess{S1-A-14}
\begin{testcase}
\caseid{S1-A0-14}
\casetitle{A title for this case}
\caseinput{
    \item A valid user
}

\casedescription{Just a sample case}
\caseapis{ 
    \item {API} 1
    \item {API} 2
}
\casesteps{
    \item Prepare
    \item Execute
}

\caseresult{
    \item The user can execute this test. I can refer to a test case with \caseref{SI-A0-14}.
    \item Everything works and works quickly.
    }
\end{testcase}

\testfailure{S1-B-9}
\begin{testcase}
\caseid{S1-B-94}
\casetitle{Another test}
\caseinput{
    \item A valid user
}

\casedescription{Just another case}
\caseapis{ 
    \item {API} 3
}
\casesteps{
    \item Prepare
    \item Execute
}

\caseresult{
    \item The user can execute this test. I can refer to a test case with \caseref{SI-A0-14}.
    }
\end{testcase}

\end{document}

这实际上给出了我想要的输出。当然,问题是我不想手动在文档中输入测试结果,这是由输出文本文件的外部测试套件生成的。我可以自由决定测试套件的输出内容(因为我控制套件),但它只会输出实际运行的测试的信息(如果不是,文档应该默认为\testnotrun),套件将生成单个文档。所以我可以让套件生成以下内容:

\testfailure{SI-B-9}

或者诸如此类的事情

\newcommand{\testsibnine}{\testfailure}

我的第一个想法是自动\testresultnamecase为每个案例生成一个命令,并将案例名称嵌入命令中(因为测试用例名称包含数字或破折号)我添加了命令\removeDashesAndNumbers,默认为\testnotrun,然后覆盖生成的文本文件中实际运行的测试的命令,但我一直没有成功。我没有简单地要求修复我多次尝试生成此类命令的其中之一(例如:

\expandafter\providecommand\csname \removeDashesAndNumbers\endcsname{\testnotrun}

),我决定提供完整的背景信息,并询问是否有人知道我该如何实现这一点?这可能是通过我已经编写的命令来帮助实现的,或者只是提供一些我没有想到的其他简单方法。

相关内容