简体   繁体   中英

not able to import ddt in python 2.7

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:\\Python27\\lib\\unittest\\case.py" , line 329 , in run testMethod() File "C:\\Python27\\lib\\unittest\\loader.py" , line 32, in testFailure raise exception ImportError: Failed to import test module: ddt class searchddt(unittest.TestCase): TypeError: 'module' object is not callable

 Process finished with exit code 1 

I believe that the issue lies in the way that you are importing needed decorators

from ddt import  data, unpack
from selenium import webdriver
import ddt

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

from ddt import ddt, data, unpack
from selenium import webdriver

There are two issues here.

The first is covered by the other answer: you need to from ddt import ddt rather than import 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") and test_search_2(2) . This isn't what you want.

You probably want something like:

@data([
    {"search_val": "phones", "expected_count": 2},
    {"search_val": "things", "expected_count": 3},
    {"search_val": "stuff", "expected_count": 4},
])
@unpack
def test_search(self, search_val, expected_count):

There's a bonus third issue, which is the indentation of your test and tearDown functions, which should not be within setUp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM