Skip to content

Commit

Permalink
docs: desc and sample code for working with unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Jun 15, 2019
1 parent be0d11c commit a2e2888
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
11 changes: 10 additions & 1 deletion docs/usage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

## 一个完整的例子

我觉得这个例子可以解决大多数问题:[sample.py](https://github.com/williamfzc/fitch/blob/master/sample/sample.py)
这个例子应该可以解决大多数问题:[sample.py](https://github.com/williamfzc/fitch/blob/master/sample/sample.py)

## 一个结合unittest的例子

与unittest一同使用的示例:[sample4unittest.py](https://github.com/williamfzc/fitch/blob/master/sample/sample4unittest.py)

暂时不推荐使用 pytest:

- unittest可定制化更强,pytest相对更适合轻量化的场景
- 目前用pytest有偶现的奇怪bug

## 创建与销毁

Expand Down
57 changes: 57 additions & 0 deletions sample/sample4unittest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import unittest

from fitch import config
from fitch.utils import restart_adb
import os
import time


# 修改配置需要在导入设备之前
config.DEFAULT_PYTHON_EXECUTOR = 'python3'
config.DEFAULT_LOCAL_PIC_DIR = os.path.join(os.path.dirname(__file__), 'pics')

DEVICE_ID = '3d33076e'

from fitch.device import FDevice


class BaseTestCase(unittest.TestCase):
device: FDevice = None

@classmethod
def setUpClass(cls):
# 重启adb服务,这里只是为了清理一下遗留进程,正式环境可以不需要
restart_adb()
cls.device = FDevice(DEVICE_ID)

@classmethod
def tearDownClass(cls):
cls.device.stop()

# 你可以在此处自定义一些业务相关的公共方法
# ...


class TestStartWechat(BaseTestCase):
WECHAT_LOGO_PATH = 'wechat_logo.png'

def test_start_wechat(self):
wechat_widget = self.device.get_widget(self.WECHAT_LOGO_PATH)
self.assertTrue(wechat_widget)

self.device.click(wechat_widget)
time.sleep(1)

wechat_widget = self.device.get_widget(self.WECHAT_LOGO_PATH)
self.assertFalse(wechat_widget)

def test_stop_wechat(self):
self.device.adb_utils.keyevent(4)
time.sleep(1)

wechat_widget = self.device.get_widget(self.WECHAT_LOGO_PATH)
self.assertTrue(wechat_widget)


if __name__ == '__main__':
unittest.main()

0 comments on commit a2e2888

Please sign in to comment.