

% add the PATH to retrieve cls and sty beamer styles files
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{../../support/beamer/}\input@path}% prepend
\makeatother

% \documentclass{pharoslides}
% \documentclass[aspectratio=169]{pharoslides} % format 16/9
\documentclass[withvideo]{pharoslides} % format 16/9 + incrustation video

\graphicspath{{./figures/}{../}{../../support/beamer/imgs/}}

%Information to be included in the title page:
\title{SUnit: Unit Tests in Pharo}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W5S06}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{Agile Programming}

Pharo programming culture adheres to agile programming

\begin{itemize}
\item Incremental, early feedback
\item Be prepared for changes
\item Driven by human
\item Supported by automated tests
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{SUnit}

\begin{itemize}
\item Extremely simple (4 classes)
\item Originally developed by K. Beck (agile programming father)
\item Got copied all over the places
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Test}

In a test, we 

\begin{itemize}
\item Create a context: Create an empty set
\item Send a stimulus: Add twice the same element
\item Check the results: Check that the set contains only one element
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Set TestCase}

\begin{listing}{}
TestCase subclass: # SetTestCase
    ...
\end{listing}

\begin{listing}{}
SetTestCase >> testAdd 
  | empty |
  empty := Set new.   "Context"
  empty add: 5.   "Stimulus"
  empty add: 5.
  self assert: empty size = 1.   "Check"

SetTestCase run: #testAdd
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{In a Subclass of TestCase}

Each method starting with \code{test*}:

\begin{itemize}
\item Represents a test
\item Is automatically executed
\end{itemize}

The results of the test are collected in a \code{TestResult} object
\end{frame}



\begin{frame}[fragile]

\frametitle{Another Example}

\begin{listing}{}
testAdjacentRunsWithEqualsAttributesAreMerged
  "this demonstrates that adjancent runs with equal attributes are merged. "
  | runArray |
  runArray := RunArray new.
  runArray
    addLast: TextEmphasis normal times: 5;
    addLast: TextEmphasis bold times: 5;
    addLast: TextEmphasis bold times: 5.
  self assert: (runArray runs size = 2).
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Failures and Errors}

\begin{itemize}
\item A \textbf{failure} is a failed assertion, i.e., an anticipated problem that you test
\item An \textbf{error} is a condition you didn't check for
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{To Test That an Error Must Be Raised}

\begin{listing}{}
SetTestCase >> removeElementNotInSet
  self 
    should: [ Set new remove: 1 ] 
    raise: NotFound
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{To Test That an Error Must Not Be Raised}

\begin{listing}{}
SetTestCase >> removeElementNotInSet
  self 
    shouldnt: [ Set new add: 1 ] 
    raise: NotFound
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Duplicating the Context}

\begin{listing}{}
SetTestCase >> testOccurrences
  | empty |
  empty := Set new.
  self 
    assert: (empty occurrencesOf: 0) 
    equals: 0.
  empty add: 5; add: 5.
  self 
    assert: (empty occurrencesOf: 5) 
    equals: 1
\end{listing}

\begin{itemize}
\item \code{empty := Set new.} is repeated between tests
\item We can factor it out
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{ setUp and tearDown Messages}

\begin{itemize}
\item Executed systematically before and after each test run
\item \code{setUp} allows us to specify and reuse the context
\item \code{tearDown} to clean after
\end{itemize}


\begin{center}
\includegraphics[width=1.0\textwidth]{figures/SetUpTearDown.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Defining a setUp Method}

\begin{listing}{}
SetTestCase >> setUp
  empty := Set new
\end{listing}

 \code{setUp} is executed for you before any test execution

\begin{listing}{}
SetTestCase >> testOccurrences
  self 
    assert: (empty occurrencesOf: 0) 
    equals: 0.
  empty add: 5; add: 5.
  self 
    assert: (empty occurrencesOf: 5) 
    equals: 1
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{SUnit Core}


\begin{center}
\includegraphics[width=0.9\textwidth]{figures/SUnitCore.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{TestSuite, TestCase and TestResult}

\begin{itemize}
\item A \code{TestCase} represents one test
\begin{itemize}
\item e.g. the method: \code{SetTestCase \textgreater{}\textgreater{} testOccurenceOf}
\end{itemize}

\item A \code{TestSuite} is a group of tests
\begin{itemize}
\item SUnit automatically builds a suite from the methods starting with \code{'test*'}
\end{itemize}

\item A \code{TestResult} represents a test execution results
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Test Resources}

\begin{itemize}
\item A \code{TestResource} is an object which is needed by a number of Test Cases, and whose instantiation is so costly in terms of time or resources that it becomes advantageous to only initialize it once for a Test Suite run.
\end{itemize}

\begin{itemize}
\item A \code{TestResource} is invoked once before any test is run. (read Pharo by Example SUnit Chapter)
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{What You Should Know}

\begin{itemize}
\item How to write simple tests
\item Reuse a bit the context by defining \code{setUp} methods
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Summary}

\begin{itemize}
\item Unit tests are easy to create and run
\item Create one test and run it million times!
\item Use them as your life insurance
\item There exists other libraries for Mock (BabyMock) or different styles of testing
\end{itemize}
\end{frame}




% \setlength{\textwidth}{\originaltextwidth}
\setbeamertemplate{background canvas}{\insertBorderForHandout}

\begin{frame}[plain]

\begin{minipage}{\originaltextwidth}
\centering
\large


\vspace{2em}

A course by

\vspace{1em}

\hspace{-20px}\includegraphics[height=32px]{inriaLogo}\hspace{3em}\raisebox{7px}{and}\hspace{3em}\raisebox{-6px}{\includegraphics[height=30px]{logoutop}}

\vspace{2em}

in collaboration with

\vspace{1em}

\includegraphics[height=40px]{unisciellogo}\hskip 1.5em%
\raisebox{5px}{\includegraphics[height=30px]{unitlogo}}\hskip 2em%
\raisebox{8px}{\includegraphics[height=24px]{univLilleLogo}}\hskip 2em%
\raisebox{3px}{\includegraphics[height=29px]{logo-IMT}}\hskip 2em%
\raisebox{1px}{\includegraphics[height=32px]{minesDouaiLogo}}

\vspace{2.5em}

\includegraphics[width=50px]{by-nc-nd} \tiny \hskip 0.5em \raisebox{5px}{Inria 2016}

\medskip

\tiny
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France

\url{https://creativecommons.org/licenses/by-nc-nd/3.0/fr/} 

\end{minipage}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
