Skip to content
Snippets Groups Projects
Commit 2eac76ff authored by alicj's avatar alicj
Browse files

update slides and code

parent a92f4025
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -199,7 +199,23 @@ No one likes to work with someone who doesn't verify/test if their code works.}
\frametitle{What is Assert?}
\begin{itemize}
\item{ Wikipedia: \say{... is a statement that a predicate (Boolean-valued function, i.e. a true-false expression) is expected to always be true at that point in the code. If an assertion evaluates to false at run time, an assertion failure results, which typically causes the program to crash, or to throw an assertion exception.}}
\item{Basically, if whatever follows assert is true, it will continue. Otherwise the test will fail and stop running.}
\item{Basically, if whatever follows assert is true, it will continue. Otherwise the test will fail and stop running.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Example of Assert?}
\begin{itemize}
\item{To assert something to be true, you can write}
\begin{lstlisting}[language=Python]
assert <true statement>
\end{lstlisting}
\item{To assert something to be false, you can write}
\begin{lstlisting}[language=Python]
assert (<false statement>) == False
\end{lstlisting}
\end{itemize}
\end{frame}
......@@ -225,14 +241,113 @@ class TestCircles:
\begin{frame}[fragile]
\frametitle{Running our first test}
\begin{itemize}
\item{Run this command in your command prompt inside the folder of your test file}
\item{\texttt{pytest test\_circles.py}}
\item{Run this command in your command prompt inside the folder of your test file: \texttt{pytest test\_circles.py}}
\item{To run all tests within the directory, just run \texttt{pytest}. It will search for all files starting with "test\_" and run all methods starting with "test\_"}
\begin{figure}[b]
{\includegraphics[width=10cm]{test-result}}
\end{figure}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{What if a test failed?}
\begin{itemize}
\item{Say we have the following code:}
\end{itemize}
\begin{lstlisting}[language=Python]
import pytest
from CircleADT import *
class TestCircles:
def test_xcoord_are_equal(self):
circle = CircleT(1,2,3)
assert circle.xcoord() == 1
def test_xcoord_are_not_equal(self):
circle = CircleT(1,2,3)
assert circle.xcoord() != 1
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{What if a test failed?}
\begin{figure}[b]
{\includegraphics[width=10cm]{test-result-fail}}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Floating Point assertsions}
\begin{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Redundant code in tests}
\begin{lstlisting}[language=Python]
import pytest
from CircleADT import *
class TestCircles:
def test_xcoord_are_equal(self):
circle = CircleT(1,2,3)
assert circle.xcoord() == 1
def test_xcoord_are_not_equal(self):
circle = CircleT(1,2,3)
assert circle.xcoord() != 1
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Redundant code in tests}
\begin{lstlisting}[language=Python]
import pytest
from CircleADT import *
class TestCircles:
def setup_method(self, method):
self.circle = CircleT(1,2,3)
def teardown_method(self, method):
self.circle = None
def test_xcoord_are_equal(self):
assert self.circle.xcoord() == 1
def test_xcoord_are_not_equal(self):
assert self.circle.xcoord() != 2
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{How much should I test?}
\begin{itemize}
\item {Test all requirements in each function}
\item{Cover edge cases that may cause unintended consequences}
\item{Have an acceptable amount of code coverage}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Code Coverage}
\begin{itemize}
\item {Test all requirements in each function}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Documentation on pytest}
\begin{itemize}
\item {\url{https://docs.pytest.org/en/latest/xunit_setup.html}}
\end{itemize}
\end{frame}
\end{document}
Tutorials/T05-PyUnit/slides/test-result-fail.png

95.2 KiB

import unittest
import pytest
from CircleADT import *
class CirclesTests(unittest.TestCase):
def setUp(self):
class TestCircles:
@classmethod
def setup_class(cls):
""" setup any state specific to the execution of the given class (which
usually contains tests).
"""
@classmethod
def teardown_class(cls):
""" teardown any state that was previously setup with a call to
setup_class.
"""
def setup_method(self, method):
self.circle = CircleT(1,2,3)
def tearDown(self):
def teardown_method(self, method):
self.circle = None
def test_xcoord_are_equal(self):
self.assertTrue(self.circle.xcoord() == 1)
assert self.circle.xcoord() == 1
def test_xcoord_are_not_equal(self):
self.assertFalse(self.circle.xcoord() != 1)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
assert self.circle.xcoord() != 2
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment