如何测试菜单
我需要通过单元测试来覆盖菜单功能,但是我正在努力获取菜单对象。
以下测试用例失败(mMenu 为空):
sendKeys(KeyEvent.KEYCODE_MENU);
mMenu = (Menu) mActivity.findViewById(com.###.###.R.id.main_menu);
assertNotNull(mMenu);
请指教。谢谢。
I need to cover Menu functionality by unit tests, however I'm struggling to obtain Menu object.
Following test case fails (mMenu is null):
sendKeys(KeyEvent.KEYCODE_MENU);
mMenu = (Menu) mActivity.findViewById(com.###.###.R.id.main_menu);
assertNotNull(mMenu);
Please advice. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的场景,并在 ActivityInstrumentationTestCase 的实现中提出了以下(非常简单)的解决方案:
这段代码做了三件事:
我希望这有帮助。
I ran into this same scenario and came up with the following (very simple) solution in my implementation of ActivityInstrumentationTestCase:
This snippet of code does three things:
I hope this helps.
您到底想测试什么?该菜单项执行正确的操作吗?
您可以调用 Activity.openOptionsMenu() 打开菜单并通过重写 onMenu 方法之一来获取对菜单的引用。此时您可以使用 Menu.performIdentifierAction 来选择菜单项。
What exactly are you trying to test? That menu items do the correct action?
You can call Activity.openOptionsMenu() to open the menu and get a reference to the menu by overriding one of the onMenu methods. At that point you can use Menu.performIdentifierAction to select menu items.
如果你想做 UI、系统或功能测试,我建议你使用 Robotium。然后您可以使用 sendKey(Solo.MENU),然后使用 clickOnText() 或 clickOnView() 单击菜单项。当你这样做时,你就可以断言正确的行为。仅仅断言它不应该为 null 是不够的。您应该检查一下 Robotium,它更适合在测试此类内容时使用。
If you want to do UI, system or function tests I would recommend you to use Robotium. Then you can just use sendKey(Solo.MENU) and then click on the menu items by using clickOnText() or clickOnView(). When you have done that you can assert right behaviour. Just asserting that it should not be null is not enough. You should check Robotium out, its way more appropriate to use when testing things like this.
使用仪器为您测试菜单项的按下情况。
这是我的一个示例测试用例,它调用“设置”菜单,该菜单启动另一个活动。
Use instrumentation to test the pressing of the menu item for you.
Here is a sample test case of mine that invokes the "Settings" menu, which starts another activity.