import unittest
from ddt import data, unpack
from selenium import webdriver
import ddt
@ddt
class searchddt(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.driver.maximize_window()
self.driver.get('https://magento.com/products/magento-commerce')
@data(("phones",2))
@unpack
def test_search(self,search_val,expected_count):
self.searchfield=self.driver.find_element_by_xpath("//I[@aria-hidden='true']/self::I")
self.searchfield.clear()
self.searchfield.send_keys(search_val)
self.searchfield.submit()
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main(verbosity=2)
FAILED (errors=1)
getting error in importing ddt and code is not executing
Error
Traceback (most recent call last):
File"C:Python27libunittestcase.py"
, line329
, in run
testMethod()
File"C:Python27libunittestloader.py"
, line 32, in testFailure
raise exception
ImportError: Failed to import test module: ddtclass searchddt(unittest.TestCase):
TypeError: ‘module’ object is not callableProcess finished with exit code 1
2
Answers
I believe that the issue lies in the way that you are importing needed decorators
If you look at the last statement, you are importing ddt which is a module, and this is causing the error when decorating the class. You need a decorator – callable that is located inside the ddt module.
I think that the solution would be to import it in the first line like this
There are two issues here.
The first is covered by the other answer: you need to
from ddt import ddt
rather thanimport ddt
.The second is that your test function is decorated with
@unpack
, so the tuple you pass to@data
will be unpacked by ddt, creating two function calls:test_search_1("phones")
andtest_search_2(2)
. This isn’t what you want.You probably want something like:
There’s a bonus third issue, which is the indentation of your test and tearDown functions, which should not be within setUp.