Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python function help
#4
Personally, I prefer to get the computer to do the work of checking the results. So, I write tests:

import unittest


class SubtractElementsTest(unittest.TestCase):
    def test_it_subtracts_all_the_elements_from_the_list(self):
        result = subtractElements([100, 20, 4, 10])

        self.assertEqual(result, 66)


if __name__ == '__main__':
    unittest.main()
Saving this in a file called test_subtract_elements.py and running it gives

Output:
$ python3 test_subtract_elements.py E ====================================================================== ERROR: test_it_subtracts_all_the_elements_from_the_list (__main__.SubtractElementsTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_subtract_elements.py", line 6, in test_it_subtracts_all_the_elements_from_the_list result = subtractElements([100, 20, 4, 10]) NameError: name 'subtractElements' is not defined ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1)
So now I can gradually make it work, using the tests for feedback.
Reply


Messages In This Thread
python function help - by hardz1n51 - Jun-08-2022, 09:54 PM
RE: python function help - by ndc85430 - Jun-09-2022, 04:56 AM
RE: python function help - by deanhystad - Jun-09-2022, 03:05 PM
RE: python function help - by rob101 - Jun-13-2022, 10:02 PM
RE: python function help - by ndc85430 - Jun-09-2022, 08:16 PM
RE: python function help - by deanhystad - Jun-09-2022, 08:25 PM
RE: python function help - by deanhystad - Jun-14-2022, 01:46 AM
RE: python function help - by rob101 - Jun-14-2022, 07:13 AM
RE: python function help - by buran - Jun-14-2022, 10:36 AM
RE: python function help - by rob101 - Jun-14-2022, 05:43 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020