truth table: Nullary and Unary Operations¶
requirements¶
how to make a python test driven development environment with truth_table
as the name of the project
Nullary Operations¶
There are 2 Nullary operations, they do not take input and always return the same value, they are singleton functions
test_logical_true¶
red: make it fail¶
I change the text in test_truth_table.py
import unittest
import src.truth_table
class TestNullaryOperations(unittest.TestCase):
def test_logical_true(self):
self.assertTrue(src.truth_table.logical_true())
and the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_true'
green: make it pass¶
I add it to the list of Exceptions encountered
# Exceptions Encountered
# AssertionError
# AttributeError
then I add a function to truth_table.py
def logical_true():
return None
the terminal shows AssertionError
AssertionError: None is not true
I change False to True in the return statement
def logical_true():
return True
and the test passes
test_logical_false¶
red: make it fail¶
I add another test
def test_logical_true(self):
self.assertTrue(src.truth_table.logical_true())
def test_logical_false(self):
self.assertFalse(src.truth_table.logical_false())
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_false'. Did you mean: 'logical_true'?
green: make it pass¶
I add a function definition to
truth_table.py
def logical_true(): return True def logical_false(): return True
and the terminal shows AssertionError
AssertionError: True is not false
When I change True to False in the return statement
def logical_false(): return False
the test passes
Unary Operations¶
There are 2 unary operations, they each take one input
test_logical_identity¶
red: make it fail¶
I add a new TestCase and a test to test_truth_table.py
class TestNullaryOperations(unittest.TestCase):
...
class TestUnaryOperations(unittest.TestCase):
def test_logical_identity(self):
self.assertTrue(src.truth_table.logical_identity(True))
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_identity'
green: make it pass¶
I add the function
def logical_false(): return False def logical_identity(): return False
the terminal shows TypeError
TypeError: logical_identity() takes 0 positional arguments but 1 was given
I add the error to the list of Exceptions encountered
# Exceptions Encountered # AssertionError # AttributeError # TypeError
then I add a parameter
def logical_identity(argument): return False
and the terminal shows AssertionError
AssertionError: False is not true
I change the return statement
def logical_identity(argument): return True
and the test passes
refactor: make it better¶
I add another line to the test
def test_logical_identity(self):
self.assertTrue(src.truth_table.logical_identity(True))
self.assertFalse(src.truth_table.logical_identity(False))
the terminal shows AssertionError
AssertionError: True is not false
when I change the return statement
def logical_identity(argument):
return False
the terminal shows AssertionError
AssertionError: False is not true
there is a failure for the line that passed before. The expectation of the test is that when True is given, the result is True and when False is given, the result is False. I make logical_identity
return its input
def logical_identity(argument):
return argument
and the terminal shows passing tests. logical_identity
is a passthrough function, it returns its input as output.
test_logical_negation¶
red: make it fail¶
I add a new test
def test_logical_identity(self):
self.assertTrue(src.truth_table.logical_identity(True))
self.assertFalse(src.truth_table.logical_identity(False))
def test_logical_negation(self):
self.assertFalse(src.truth_table.logical_negation(True))
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_negation'
green: make it pass¶
I add a definition for it
def logical_identity(argument): return argument def logical_negation(argument): return argument
the terminal shows AssertionError
AssertionError: True is not false
when I make it return False
def logical_negation(argument): return False
the terminal shows passing tests
refactor: make it better¶
I add the next case
def test_logical_negation(self): self.assertFalse(src.truth_table.logical_negation(True)) self.assertTrue(src.truth_table.logical_negation(False))
the terminal shows AssertionError
AssertionError: False is not true
I change the return statement
def logical_negation(argument): return True
and the terminal shows AssertionError
AssertionError: True is not false
it fails for the line that passed before. When I make the function return its input
def logical_negation(argument): return argument
the terminal shows AssertionError
AssertionError: True is not false
the expectation of the test is that when True is given, the result is False and when False is given, the result is True, I can make that happen with the not keyword
def logical_negation(argument): return not argument
and the terminal shows passing tests.
logical_negation
returns the opposite of its inputI change the name of the test
def test_logical_negation_aka_not(self): self.assertFalse(src.truth_table.logical_negation(True)) self.assertFalse(src.truth_table.logical_negation(False))
review¶
I ran tests for Nullary and Unary operations. Would you like to test binary operations?