Unittest errors

Answered

I'm trying to run basic unit tests, test which through my terminal will complete fine without error but when run (ctrl+shift+f10) through Pycharm Pro 2017.1.3 end in an error message of [below] on Fedora 25. Before this i was on Pycharm Edu with similar issues. It seems virtuall impossible right now to use unittests.

/usr/bin/python3.5 /home/Ithaca/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/171.4424.42/helpers/pycharm/_jb_nosetest_runner.py --path "/home/Ithaca/PycharmProjects/PythonCrashCourse2016/Ch11: Testing your code/11-1 city, country.py"
Testing started at 1:58 AM ...
Traceback (most recent call last):
  File "/home/Ithaca/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/171.4424.42/helpers/pycharm/_jb_nosetest_runner.py", line 4, in <module>
    import nose
ImportError: No module named 'nose'

Process finished with exit code 1

8 comments
Comment actions Permalink

Hi Alex-hivento! Looks like you have `nose` set as a test runner, could you please do the following:

  • Switch to unittest in Settings | Tools | Python Integrated Tools | Default Test Runner
  • Remove existed Test Run Configuration
  • Try to execute tests once more
0
Comment actions Permalink

We seemed to have progressed further but now it is saying zero tests were run.

/usr/bin/python3.5 /home/Ithaca/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/171.4424.42/helpers/pycharm/_jb_unittest_runner.py --path "/home/Ithaca/PycharmProjects/PythonCrashCourse2016/Ch11: Testing your code/test_cities.py"
Testing started at 12:26 PM ...
Launching unittests with arguments python -m unittest /home/Ithaca/PycharmProjects/PythonCrashCourse2016/Ch11: Testing your code/test_cities.py in /home/Ithaca/PycharmProjects/PythonCrashCourse2016/Ch11: Testing your code

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Process finished with exit code 0
Empty test suite.

0
Comment actions Permalink

Could you please take a screenshot of the project structure? Are you able to run unittest from the console to execute this tests?

0
Comment actions Permalink

/home/Ithaca/PycharmProjects/PythonCrashCourse2016/'Ch11: Testing your code'/test_cities.py is a unittest ive tried to run.

Not sure how to do the last part you asked? Copy selected code and alt+shift+e ?

0
Comment actions Permalink

Ah. I see, you launch the test file as a script. The problem here is PyCharm use

python -m unittest

to execute tests, you can try it in the terminal. And unfortunately it fails because you have

unittest.main()

instruction on the last line. Try to place it inside if statement:

if __name__ == '__main__':

    unittest.main()

this is a common pattern and it will trigger main function only when you execute the file as a script (e.g. python sample.py) and not when you run unittest test discovering.

2
Comment actions Permalink

I am trying to run my code. Although it is not showing any error still code is not running. It is showing in output as 'Ran 0 tests'. Previously it was running fine but all of sudden it stopped.
Sharing the screenshot of the code along with the output. I am new to pycharm.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import HtmlTestRunner
import unittest
# import urllib3
# import re

class EpikView(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome\
(r"C:/Users/Arpita Sharma/PycharmProjects/Epikso_Gov/browserdrivers/chromedriver.exe")
cls.driver.set_page_load_timeout(30)
cls.driver.get("https://epiksolution.org/epikviewdemo/")
cls.driver.maximize_window()
cls.driver.implicitly_wait(10)

def test_logo(self):
logo = self.driver.find_element_by_xpath('//*[@id="page-header-inner"]/div/div/div/div/div[1]/div/a/img[1]')
print(logo.is_displayed()) # verify if logo is displayed or not
logo.click()
print("Title of Home Page is : " + self.driver.title) # returns title of homepage
homePage_url = self.driver.current_url
print("Home Page URL is : " + homePage_url) # returns URL of home page

def test_header_homepage(self):
headerLink_home = self.driver.find_element_by_xpath('//*[@id="menu-item-421"]/a/div/span')
headerLink_home.click()
homePage_url = self.driver.current_url
print("Home Page URL is : " + homePage_url) # returns URL of home page

#driver.implicitly_wait(15)

def test_header_about(self):
headerLink_aboutUs = self.driver.find_element_by_xpath('//*[@id="menu-item-396"]/a')
headerLink_aboutUs.click()
about_url = self.driver.current_url
print("About Us Page URL is : " + about_url) # returns URL of about us page
print("Title of About Us Page is : " + self.driver.title) # returns title of about us page
# driver.implicitly_wait(15)

def test_header_contact(self):
headerLink_contact = self.driver.find_element_by_xpath('//*[@id="menu-item-420"]/a/div/span')
headerLink_contact.click()
contact_url = self.driver.current_url
print("Contact Us Page URL is : " + contact_url) # returns URL of contact page
print("Title of contact us Page is : " + self.driver.title) # returns title of contact page

def test_contactpageform(self):
self.driver.get("https://epiksolution.org/epikviewdemo/contact-us/")
self.driver.find_element_by_name('your-name').send_keys("Arpita")
self.driver.find_element_by_name('your-email').send_keys("arpita@mailinator.com")
self.driver.find_element_by_name('your-tel').send_keys("9898989898")
self.driver.find_element_by_name('your-message').send_keys('Welcome to Epik view 360 message')
self.driver.find_element_by_xpath('//*[@id="wpcf7-f7-p417-o1"]/form/p[5]/input').click()
address_text = self.driver.find_element_by_class_name('content')
print(address_text.text)

def test_bookappoint_btn(self):
btn_Bookappoint = self.driver.find_element_by_xpath('//*[@id="header-right-inner"]/div/a[1]')
btn_Bookappoint.click()
bookAppoint_url = self.driver.current_url
print("Book An Appointment Page URL is : " + bookAppoint_url) # returns URL of Book an appointment page
print("Title of Home Page is : " + self.driver.title) # returns title of Book an Appointment page

@classmethod
def tearDownClass(cls):
cls.driver.close()
cls.driver.quit()
print("Test Completed")

if __name__ == '__main__':
unittest.main(
testRunner=HtmlTestRunner.HTMLTestRunner(output='C:/Users/Arpita Sharma/PycharmProjects/Epikso_Gov/Reports')
)

0
Comment actions Permalink
if __name__ == '__main__':
  unittest.main()

Adding this instead of unittest.main() alone works perfectly fine.Pavel Karateev...great.though i have not the
slightest idea how or why...a good explanation if you don't mind.
0

Please sign in to leave a comment.