pytest 运行报错Launching pytest with arguments record_test.py::RecordTest --no-header --no-summary -q in

问题描述:

Launching pytest with arguments record_test.py::RecordTest --no-header --no-summary -q in /Users/nhl/Desktop/软件测试/Pycharm/appium_Demo1

报错原因:

类名错误

解决办法:

修改类名:
类名必须是 Test开头,比如:class TestRecord:class TestApiDemo:

pytest.py测试脚本的命名规范:

  1. 类:Test开头
    类名必须以Test 开头,比如:class TestRecord:class TestApiDemo:

  2. 方法名:test_开头:
    测试方法名比如以 test_开头,比如:def test_api_demo(self):
    测试了一下 test 开头也可以,不用下划线也行,但是不能大写 Test 开头!,不过为了规范好看,建议 test_开头。

  3. 代码对齐规范!
    代码必须对齐才行,否则无法正常运行,比如
    类class和下面的方法要错位一个 Tab 等,可收缩大括号看格式显示。
    mac 代码对齐快捷键:

  • 选中对齐代码, tab键,可使选中全部代码全部右移一个 tab,常用于选中某个方法下全部代码,错格对齐,代码对齐都是错一个 tab 距离。
  • 选中代码,command +option+L,
    具体显示如下:

# class TestApiDemo:
class TestRecord:

    def setup_method(self):
        # 设置 capability
        caps = {
            # 设置 app 安装的平台(Android、iOS)
            "platformName": "android",
            # 设置 appium 驱动
            "appium:automationName": "uiautomator2",
            # 设置设备名称
            "appium:deviceName": "127.0.0.1:62001",
            # 设置 app 的包名
            "appium:appPackage": "io.appium.android.apis",
            # 设置 app 启动页
            "appium:appActivity": ".ApiDemos"
        }
        # 初始化 driver
        self.driver = webdriver.Remote(
            "http://127.0.0.1:4723",
            options=UiAutomator2Options().load_capabilities(caps)
        )

    def teardown_method(self):
        # 关闭 driver
        self.driver.quit()

    def test_api_demo(self):
        # 测试方法名必须 test_
        # def record_test(self):
        # def test_record(self):
        # 测试步骤
        # 找到 0s 元素
        el1 = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="OS")
        # 点击 os 元素
        el1.click()
        # 找到 Morse Code 元素
        el2 = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="Morse Code")
        # 点击Morse Code 元素
        el2.click()
        # 找到输入框元素
        el3 = self.driver.find_element(by=AppiumBy.ID, value="io.appium.android.apis:id/text")
        # 在输入框中输入内容
        el3.send_keys("测试人")
        # 点击返回按钮
        self.driver.back()
        # 点击返回按钮
        self.driver.back()

        # 断言,判断当前页面中第一个元素的文本为 Access'ibility
        result = self.driver.find_element(AppiumBy.XPATH, "//*[@resource-id='android:id/text1'][1]")
        print(result.text)

        assert result.text == "Access'ibility"

  1. 文件名 test_开头或结尾
    试了一下,文件名是什么,什么格式不影响运行效果,为了规范好看,建议 test_开头。

注:

  • 修改代码错误,直至在类Test方法test_前出现绿色的运行按钮才可。
  • 出现绿色运行按钮是 pytest 环境配置问题,此处就不赘述,只解决环境都好的情况下测试脚本,appium、模拟器均开启状态。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容