0

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
1
  • 2
    It should be if __name__ == "__main__": and all you have to do is call unittest.main(). You don't need to instantiate your class. The unittest.main() handles everything for you. Revise the documentation. Commented Oct 13, 2016 at 1:29

2 Answers 2

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
answered Oct 13, 2016 at 1:43
Sign up to request clarification or add additional context in comments.

Comments

1

In your test you need two things:

  1. Define your test function with 'test'
  2. 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
answered Oct 13, 2016 at 3:38

1 Comment

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()

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.