ImportError: Start directory is not importable: './test_case'怎麼解決?已附上詳細程式碼和專案結果說明。

jerrygo123發表於2020-09-13

1、Pge object封裝:youdao_page.py存放於Page資料夾。
from poium import Page, NewPageElement
class YoudaoPage(Page):
search_box = NewPageElement(id_ = "com.youdao.dict:id/search_input_box")
search_button = NewPageElement(class_name = "android.widget.ImageView")
search_result =NewPageElement(id_ = "com.youdao.dict:id/tv_word")

2、APP啟動模組封裝:my_test.py存放於common資料夾。
import unittest,time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import sys
from os.path import dirname, abspath
BASE_PATH = dirname(dirname(abspath(file)))
sys.path.append(BASE_PATH)

caps = {}
caps["deviceName"] = "HWJSN-HW"
caps["automationName"] = "appium"
caps["platformVersion"] = "10"
caps["platformName"] = "Android"
caps["appPackage"] = "com.youdao.dict"
caps["appActivity"] = ".activity.MainActivity"
caps["ensureWebviewsHavePages"] = True

class MyTest(unittest.TestCase):

@classmethod
def setUpClass(cls):

cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
cls.driver.implicitly_wait(10)

try:
loc = ("xpath", "//*[@text='始終允許']")
el = WebDriverWait(cls.driver, 5, 0.5).until(EC.presence_of_element_located(loc))
TouchAction(cls.driver).tap(el).perform()
except:
pass

@classmethod
def tearDownClass(cls):

cls.driver.quit()

if name=="main":
unittest.main()

3、測試用列封裝:test_word_search.py存放於test_case資料夾:
import unittest
import sys
from os.path import dirname, abspath
BASE_PATH = dirname(dirname(abspath(file)))
sys.path.append(BASE_PATH)
from common.my_test import MyTest
from page.youdao_page import YoudaoPage

class TestYoudaoSearch(MyTest):

def test_search_clear(self):
"""定義搜尋關鍵字:clear"""
page = YoudaoPage(self.driver)
page.search_box.click()
page.search_box = "clear"
page.search_button.click()
print(page.search_result.text)
self.assertEqual("clear", page.search_result.text)

if name=="main":
unittest.main()

4、執行測試封裝:run_tests.py存放於test_report資料夾。
import unittest
import time
import yagmail
from HTMLTestRunner import HTMLTestRunner

def send_mail(report):
yag = yagmail.SMTP(user = "%%%%@163.com",
password = "XESWYPMRUGLMIOYY",
host = "smtp.163.com")

subject = "主題:自動化測試報告"
contents = "請檢視郵件"
yag.send("%%%%@163.com",subject, contents, report)
print("email has send out")

if name == "main":
test_dir = './test_case'
suit = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')

now_time = time.strftime("%Y-%m-%d %H_%M_%S")
html_report = now_time + "youdao.html"
with open(html_report,"wb") as fp:
runner = HTMLTestRunner(stream=fp, title="youdao",
description="android",
verbosity=2)
runner.run(suit)
fp.close()

4、以上所有資料夾都存放於appium project資料夾內,每個資料夾也放置了 名為init.py的空檔案。
但執行測試回報如題所示的錯誤。請問是什麼原因造成的,怎麼解決?

相關文章