I have written the below code to test a basic unittest case for learning. When I execute the below code. I do not get any output. Could someone let me know what could be issue.
import unittest
class test123(unittest.TestCase):
def test1(self):
print "test1"
if __name__ == "main":
x=test123()
x.test1()
unittest.main()
martineau
124k29 gold badges181 silver badges319 bronze badges
asked Oct 13, 2016 at 1:22
Baslingappa Bhujang
811 gold badge1 silver badge3 bronze badges
2 Answers 2
your code should look like this:
import unittest
class test123(unittest.TestCase):
def test1(self):
print "test1"
if __name__ == "__main__":
unittest.main()
hence it is name and main with two underscores at the start and end, when you change it and run it with your code then you will get an error with using:
x = test123()
x.test1()
ValueError: no such test method in <class '__main__.test123'>: runTest
Sign up to request clarification or add additional context in comments.
Comments
In your test you need two things:
- Define your test function with 'test'
- You need a expected result
test.py
import unittest
class TestHello(unittest.TestCase):
def test_hello(self): # Your test function usually need define her name with test
str_hello = 'hello'
self.assertEqual(str_hello, 'hello') # you need return a expected result
def test_split(self):
str_hello = 'hello world'
self.assertEqual(str_hello.split(), ['hello', 'world'])
if __name__ == '__main__':
unittest.main()
for execute use:
python -m unittest test
out:
stackoverflow$ python -m unittest test
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
1 Comment
Baslingappa Bhujang
import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class weblogin(unittest.TestCase): def open_fb(self): driver = webdriver.Firefox() driver.get("facebook.com") email_field=driver.find_element_by_id("email") email_field.send_keys("[email protected]") pass_field=driver.find_element_by_id("pass") pass_field.send_keys("test123") pass_field.send_keys(Keys.RETURN) time.sleep(5) if name == 'main': unittest.main()
lang-py
if __name__ == "__main__":and all you have to do is callunittest.main(). You don't need to instantiate your class. Theunittest.main()handles everything for you. Revise the documentation.