移动测试基础 python+unittest 多用例测试执行失败的问题

淼淼淼 · 2017年06月08日 · 最后由 yajunzheng 回复于 2018年09月27日 · 2744 次阅读

背景

自己写了一个测试框架,在多用例执行的时候每次,执行成功目录的第一个用例后,后面的用例均失败。

目录结构

D:\AUTOMEDLINKER
├─.idea
├─apk
├─common
├─log
├─medcases
│  ├─ChannelTest
│  ├─HeadlineTest
│  └─LoginTest
├─report
└─screenshot

用例运行代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by nil_mmm
import unittest
import HTMLTestRunner
import time

# 配置Case的路径
case_dir = [
    'LoginTest',
    'ChannelTest']
# 配置报告名称
filename = "D:/automedlinker/report/" + time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) + ".html"
# 配置报告标题
report_title = 'Auto Case Report'
# 配置报告描述
report_description = ' '


if __name__ == '__main__':
    suite = unittest.TestSuite()
    for cases in case_dir:
        cases = unittest.defaultTestLoader.discover(cases, pattern='*Test.py', top_level_dir='medcases')
        suite.addTests(cases)
    fp = file(filename, 'wb')
     #使用 HTMLTestRunner 配置参数,输出报告路径、报告标题、描述
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=report_title, description=report_description)
    runner.run(suite)

test_case 编写结构 附我的一个模板用例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by nil_mmm
import unittest
import android_caps
import traceback
from common import Main
from common import GetScreen
from common import UserLogin

class Case(unittest.TestCase):
    def setUp(self):
        self.driver = android_caps.my_driver
        print '第一个用例检测'

    def tearDown(self):
        self.driver.quit()

    def test_first(self):
        try:
            UserLogin.jump_session_page()
            print '进入搜索界面'
            Main.get_resource_id('tv_search').click()
            Main.get_resource_id('et_search_action').send_keys('test')
            print '搜索test'
            Main.get_resource_id('tv_search').click()
            Main.get_resource_id('tv_title')
        except:
            GetScreen.get_screenshot(self.driver,'test')
            traceback.print_exc()
            self.fail()



输出报告

问题

  • 找了好久没找到原因.因为单个用例都是可以执行的。
  • 万分感谢提供帮助的同学.
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 10 条回复 时间 点赞

看样 appium session 挂掉了,排除框架本身,用命令行或者 IDE,能执行多个 case 成功不?

cukes 回复

多个 case 不行

淼淼淼 回复

那可能跟你使用和管理 driver 的方式会有一定关系,可以简化代码,缩小模型了,再看看

cukes 回复

好的 感谢 我再看看

可以贴一些 appium 的 log 或者把你的 setup 方法贴出来看看。

我之前遇到一次,我把 try except 当时去掉。然后试过貌似行了,好久不弄了,

Kun 回复

appiumlog

[debug] [ADB] Pressing the HOME button
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'D:\Users\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","192.168.134.1
01:5555","shell","input","keyevent",3]
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [UiAutomator] Shutting down UiAutomator
[debug] [UiAutomator] Moving to state 'stopping'
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Closed client connection
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: numtests=1
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: stream=.
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: current=1
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS_CODE: 0
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: stream=
[debug] [AndroidBootstrap] [UIAUTO STDOUT] Test results for WatcherResultPrinter=.
[debug] [AndroidBootstrap] [UIAUTO STDOUT] Time: 18.601
[debug] [AndroidBootstrap] [UIAUTO STDOUT] OK (1 test)
[debug] [AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS_CODE: -1
[debug] [UiAutomator] UiAutomator shut down normally
[debug] [UiAutomator] Moving to state 'stopped'
[debug] [ADB] Attempting to kill all uiautomator processes
[debug] [ADB] Getting all processes with uiautomator
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'D:\Users\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","192.168.134.1
01:5555","shell","ps"]
[ADB] No uiautomator process found to kill, continuing...
[debug] [UiAutomator] Moving to state 'stopped'
[debug] [Logcat] Stopping logcat capture
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'D:\Users\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","192.168.134.1
01:5555","shell","am","force-stop","io.appium.unlock"]
[debug] [AndroidDriver] Not cleaning generated files. Add `clearSystemFiles` capability if wanted.
[Appium] Removing session 50f32436-b1c1-4369-b72f-2c6fdeb3ec7d from our master session list
[debug] [BaseDriver] Event 'quitSessionFinished' logged at 1496979735883 (11:42:15 GMT+0800 (中国标准时间))
[debug] [MJSONWP] Received response: null
[debug] [MJSONWP] But deleting session, so not returning
[debug] [MJSONWP] Responding to client with driver.deleteSession() result: null
[HTTP] <-- DELETE /wd/hub/session/50f32436-b1c1-4369-b72f-2c6fdeb3ec7d 200 1572 ms - 76
[HTTP] --> POST /wd/hub/session/50f32436-b1c1-4369-b72f-2c6fdeb3ec7d/element {"using":"id","sessionId":"50f32436-b1c1
-4369-b72f-2c6fdeb3ec7d","value":"right_text"}
[HTTP] <-- POST /wd/hub/session/50f32436-b1c1-4369-b72f-2c6fdeb3ec7d/element 404 3 ms - 131
[HTTP] --> GET /wd/hub/session/50f32436-b1c1-4369-b72f-2c6fdeb3ec7d/screenshot {}
[HTTP] <-- GET /wd/hub/session/50f32436-b1c1-4369-b72f-2c6fdeb3ec7d/screenshot 404 2 ms - 131
[HTTP] --> DELETE /wd/hub/session/50f32436-b1c1-4369-b72f-2c6fdeb3ec7d {}
[HTTP] <-- DELETE /wd/hub/session/50f32436-b1c1-4369-b72f-2c6fdeb3ec7d 404 3 ms - 131

'D:\Users\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","192.168.134.101:5555","shell","input","keyevent",3] 看起来你似乎在用 Grid hub,那你先检查一下http://192.168.134.101:4444/grid/console 上你的手机有没有注册到 Hub。
另外可以贴 android_caps.my_driver 这个方法。

Kun 回复

先贴方法

desired_caps = {
        'platformName': Platform,
        'platformVersion': PlatformVersion,
        'deviceName': DeviceName,
        'app': ApkPATH,
        'unicodeKeyboard': True,
        'resetKeyboard': True,
        'udid': UdId,
        'appPackage': PackageName,
        # 'appActivity': FirstActivity,
        'noRest': True
}

my_driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
my_driver.implicitly_wait(TIME_TO_WAIT)
淼淼淼 回复

亲 解决了吗 可以提供个思路吗

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册