本文主要是介绍测试驱动开发(TTD),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
测试应用有很多方法,例如,黑盒测试、白盒测试、迭代测试等,然而,这些方法都是从宏观上描述测试的。为了在技术上保障测试的效果,Kent Beck(也是极限编程创始人)提出了在结果上进行限制的测试方法,也就是在编写程序之前,先确定程序中的变量、控件等元素允许的值。如果在编写程序时,变量、控件中的值与事先确定的值不相符,就说明程序的某处有bug,这种测试方法就是TDD(Test Driver Development,测试驱动开发)。TDD和Opengl ES一样,并不是具体的软件或程序库,只是一套测试框架(Junit),可用于对Android应用程序进行TDD测试。
Android SDK提供的测试框架,该测试框架基于Junit。测试框架不仅可以测试普通的java类(继承AndroidTestcase),也可以测试Android的应用程序组建,如Activity(ActivityInstrumentationTestCase2<>)、ContentProvider(ProviderTestCase<>)、和Service(ServiceTestCase<>)。
The testing framework has these key features:
- Android test suites are based on JUnit. You can use plain JUnit to test a class that doesn't call the Android API, or Android's JUnit extensions to test Android components. If you're new to Android testing, you can start with general-purpose test case classes such as AndroidTestCase and then go on to use more sophisticated classes.
- The Android JUnit extensions provide component-specific test case classes. These classes provide helper methods for creating mock objects and methods that help you control the lifecycle of a component.
- Test suites are contained in test packages that are similar to main application packages, so you don't need to learn a new set of tools or techniques for designing and building tests.
- The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDEs. These tools get information from the project of the application under test and use this information to automatically create the build files, manifest file, and directory structure for the test package.
- The SDK also provides monkeyrunner, an API for testing devices with Python programs, and UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.
测试类型 | 继承的类 | 获取测试对象的代码 |
Activity | ActivityInstrumenttation | activity = this.getActivity() |
ContentProvider | ProviderTestCase2 | contentProvider = getProvider() |
Service | ServiceTestCase | Intent intent = new Intent(mContext,MyService.class); StartService(intent); service = getService(); |
普通Java类 | AndroidTestCase | myClass = new MyClass(); |
这篇关于测试驱动开发(TTD)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!