From a9b6880cd4fe47bc4e57d941eda480ff08329211 Mon Sep 17 00:00:00 2001 From: francesco wang Date: Mon, 20 Feb 2023 23:11:30 +0000 Subject: [PATCH 1/2] Add unit test examples --- curriculum/assertEqual.py | 55 ++++++++++++++++++++++++++++ curriculum/assertFalse.py | 19 ++++++++++ curriculum/assertNotEqual.py | 15 ++++++++ curriculum/assertTrue.py | 19 ++++++++++ curriculum/unit-testing-framework.md | 16 ++++++++ 5 files changed, 124 insertions(+) create mode 100644 curriculum/assertEqual.py create mode 100644 curriculum/assertFalse.py create mode 100644 curriculum/assertNotEqual.py create mode 100644 curriculum/assertTrue.py create mode 100644 curriculum/unit-testing-framework.md diff --git a/curriculum/assertEqual.py b/curriculum/assertEqual.py new file mode 100644 index 00000000..e20978dc --- /dev/null +++ b/curriculum/assertEqual.py @@ -0,0 +1,55 @@ +import unittest + + +def add(a, b): + return a + b + + +def subtract(a, b): + return a - b + + +def multiplication(a, b): + return a * b + + +def division(a, b): + return a / b + + +def modulus(a, b): + return a % b + + +class TestAddition(unittest.TestCase): + def test_addition(self): + result = add(2, 4) + self.assertEqual(result, 6) + + +class TestSubtraction(unittest.TestCase): + def test_subtraction(self): + result = subtract(10, 2) + self.assertEqual(result, 8) + + +class TestMultiplication(unittest.TestCase): + def test_multiplication(self): + result = multiplication(5, 3) + self.assertEqual(result, 15) + + +class TestDivision(unittest.TestCase): + def test_division(self): + result = division(7, 2) + self.assertEqual(result, 3.5) + + +class TestModulus(unittest.TestCase): + def test_modulus(self): + result = modulus(10, 3) + self.assertEqual(result, 1) + + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/assertFalse.py b/curriculum/assertFalse.py new file mode 100644 index 00000000..ecc40e68 --- /dev/null +++ b/curriculum/assertFalse.py @@ -0,0 +1,19 @@ +import unittest + + +def is_negative(number): + return number < 0 + + +class TestIsNegative(unittest.TestCase): + def test_negative_number(self): + result = is_negative(-8) + self.assertTrue(result, "-8 is a negative number") + + def test_positive_number(self): + result = is_negative(5) + self.assertFalse(result, "8 is not a negative number") + + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/assertNotEqual.py b/curriculum/assertNotEqual.py new file mode 100644 index 00000000..9e3df074 --- /dev/null +++ b/curriculum/assertNotEqual.py @@ -0,0 +1,15 @@ +import unittest + + +def compare_numbers(a, b): + return a != b + + +class TestCompareNumbers(unittest.TestCase): + def test_compare_numbers(self): + result = compare_numbers(10, 20) + self.assertNotEqual(result, False) + + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/assertTrue.py b/curriculum/assertTrue.py new file mode 100644 index 00000000..f310a317 --- /dev/null +++ b/curriculum/assertTrue.py @@ -0,0 +1,19 @@ +import unittest + + +def is_positive(number): + return number > 0 + + +class TestIsPositive(unittest.TestCase): + def test_positive_number(self): + result = is_positive(7) + self.assertTrue(result, "7 is a positive number") + + def test_negative_number(self): + result = is_positive(-7) + self.assertFalse(result, "-7 is not a positive number") + + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/unit-testing-framework.md b/curriculum/unit-testing-framework.md new file mode 100644 index 00000000..04048952 --- /dev/null +++ b/curriculum/unit-testing-framework.md @@ -0,0 +1,16 @@ + + +

Unit Testing Framework

+ +
    +
  1. assertAlmostEqual(a, b)
  2. +
  3. assertNotEqual(a, b)
  4. +
  5. assertTrue(x)
  6. +
  7. assertFalse(x)
  8. +
  9. assertIs(a, b)
  10. +
  11. assertIsNot(a, b)
  12. +
  13. assertIn(a, b)
  14. +
  15. assertNotIn(a, b))
  16. +
+ + \ No newline at end of file From 3e48976e07d28d539a9c5472d541225c6603faa7 Mon Sep 17 00:00:00 2001 From: francesco wang Date: Tue, 21 Feb 2023 22:27:06 +0000 Subject: [PATCH 2/2] Add more unit test examples --- curriculum/assertAlmostEqual.py | 12 ++++++++++++ curriculum/assertIn.py | 12 ++++++++++++ curriculum/assertIs.py | 12 ++++++++++++ curriculum/assertIsNot.py | 12 ++++++++++++ curriculum/assertNotIn.py | 12 ++++++++++++ curriculum/unit-testing-framework.md | 1 + 6 files changed, 61 insertions(+) create mode 100644 curriculum/assertAlmostEqual.py create mode 100644 curriculum/assertIn.py create mode 100644 curriculum/assertIs.py create mode 100644 curriculum/assertIsNot.py create mode 100644 curriculum/assertNotIn.py diff --git a/curriculum/assertAlmostEqual.py b/curriculum/assertAlmostEqual.py new file mode 100644 index 00000000..198ae7dd --- /dev/null +++ b/curriculum/assertAlmostEqual.py @@ -0,0 +1,12 @@ +import unittest + +def calculate_pi(): + return 3.14159265358979323846 + +class TestCalculatePi(unittest.TestCase): + def test_calculate_pi(self): + result = calculate_pi() + self.assertAlmostEqual(result, 3.14, places=2) + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/assertIn.py b/curriculum/assertIn.py new file mode 100644 index 00000000..db8280be --- /dev/null +++ b/curriculum/assertIn.py @@ -0,0 +1,12 @@ +import unittest + +def find_value(value, lst): + return value in lst + +class TestFindValue(unittest.TestCase): + def test_find_value(self): + result = find_value(2, [1, 2, 3, 4, 5]) + self.assertIn(result, [True, False]) + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/assertIs.py b/curriculum/assertIs.py new file mode 100644 index 00000000..41be9fc1 --- /dev/null +++ b/curriculum/assertIs.py @@ -0,0 +1,12 @@ +import unittest + +def return_list(): + return [] + +class TestReturnList(unittest.TestCase): + def test_return_list(self): + result = return_list() + self.assertIs(result, []) + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/assertIsNot.py b/curriculum/assertIsNot.py new file mode 100644 index 00000000..a3d6b773 --- /dev/null +++ b/curriculum/assertIsNot.py @@ -0,0 +1,12 @@ +import unittest + +def return_list(): + return [] + +class TestReturnList(unittest.TestCase): + def test_return_list(self): + result = return_list() + self.assertIsNot(result, [1, 2, 3]) + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/assertNotIn.py b/curriculum/assertNotIn.py new file mode 100644 index 00000000..10130e4a --- /dev/null +++ b/curriculum/assertNotIn.py @@ -0,0 +1,12 @@ +import unittest + +def find_value(value, lst): + return value in lst + +class TestFindValue(unittest.TestCase): + def test_find_value(self): + result = find_value(0, [1, 2, 3, 4, 5]) + self.assertNotIn(result, [True]) + +if __name__ == '__main__': + unittest.main() diff --git a/curriculum/unit-testing-framework.md b/curriculum/unit-testing-framework.md index 04048952..34931295 100644 --- a/curriculum/unit-testing-framework.md +++ b/curriculum/unit-testing-framework.md @@ -3,6 +3,7 @@

Unit Testing Framework

    +
  1. assertEqual(a, b)
  2. assertAlmostEqual(a, b)
  3. assertNotEqual(a, b)
  4. assertTrue(x)