Python Tutorial: Unit Testing Your Code with the unittest Module
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about getting started with Python's unittest, since many questions say they are following this walkthrough.
Recommended by 2 people across 2 different months, 2019 to 2021.
What it is
A 39-minute Python tutorial by Corey Schafer on writing unit tests with the built-in unittest module. It walks through test cases, assert methods and setup, using a small calculator-style example, and links code samples on GitHub.
What people said
Recommends Sal-laS · 2019-09 · Stack Overflow · on “Recognizing module level tests”
@RoyM I think, you are using a wrong practice to write test. i suggest you to watch this video and edit your question. [this video] For sure, i will help you after you watch this video
Recommends Mandera · 2020-05 · Stack Overflow · on “Testing creation of a global variable using globals() in python unittest”
I think the globals in the function scope are different from the globals in the test scope. But regardless I think it's bad practice to access the global scope from a unittest since they should be isolated. If you really wanna do it you'll need to send the globals from the test as a parameter. There is also a thing called Mock, I haven't tried it myself but I saw a good video on it. It basically lets you simulate a http request and such: [this video]
Cites KED · 2019-08 · Stack Overflow · on “Python unit test patch failing”
So I was following this youtube video: [this video] When I replicate what is done in the video, my test fails: Class: import requests class JetpackRebooter: @staticmethod def reboot_jetpack(secure_token): response = requests.post(' [LINK] ', data={'gSecureToken' : secure_token}) return response.status_code Unit test: from src.jetpack_rebooter import JetpackRebooter from unittest.mock import patch def test_reboot_jetpack(): secure_token = 'ca26bacf85c6d69d0bdaa3ff07df3cc4118abf45' with patch("jetpack_rebooter.requests.post") as mocked_post: mocked_post.return_value.status_code = 200 assert JetpackRebooter.reboot_jetpack(secure_token) == 200 However , changing patch("jetpack_rebooter.requests.post"
Cites Fafa · 2019-11 · Stack Overflow · on “AttributeError: module 'unittest' has no attribute 'Testcase'”
I am new with python, i am trying to fllow the tutorial on youtube [this video] I have the error message code: import unittest import calc class TestCalc(unittest.Testcase): def test_add(self): result=calc.add(10,5) self.assertEqual(result,15) suite = unittest.TestLoader().loadTestsFromTestCase(TestCalc) unittest.TextTestRunner(verbosity=2).run(suite) I already seen all the answer on stackoverflow but the error resist. Please help
Cites MrNobody33 · 2020-06 · Stack Overflow · on “Python unit test running error using flask_restful”
That's because the last lines of code, if __name__ == '__main__': , have the purpose of running testing files like this python tests/test_vuln.py . If you want to run this program python -m unittest tests/test_vuln.py , try erasing the lines above. So: With: if __name__ == '__main__': unittest.main() Run the script with the following command: python tests/test_vuln.py . And without it, run the script with the following command: python -m unittest tests/test_vuln.py . Helpful info in link 1 [this video] .
Cites Justin · 2021-01 · Stack Overflow · on “Why do we need to wrap test functions in a class?”
I was studying unittest by following the examples here [this video] . In the following code, def test_add() is supposed to be wrapped in class testClass() , but for my curiosity, I didn't encapsulate it. # class testClass(unittest.TestCase): def test_add(self): result = cal_fun.add_fuc(5, 10) self.assertEqual(result, 15) if __name__ == '__main__': unittest.main() The result came out in VScode as: ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK Why was no test run? Why must the def test_add() be wrapped in a class ?