Presentation is loading. Please wait.

Presentation is loading. Please wait.

Automated Unit Testing. Test Automation Manual testing is laborious and time consuming. Computer automation has transformed many sectors of our economy.

Similar presentations


Presentation on theme: "Automated Unit Testing. Test Automation Manual testing is laborious and time consuming. Computer automation has transformed many sectors of our economy."— Presentation transcript:

1 Automated Unit Testing

2 Test Automation Manual testing is laborious and time consuming. Computer automation has transformed many sectors of our economy. Why not direct the same technology at the laborious aspects of software testing? Test automation is the process of using software to automate the manual aspects of software testing. Automated testing reduces labor required to run tests and speeds up the testing process. Perhaps the best opportunity for automated testing is during regression testing. Developers generally feel more confident making a change when there is a large comprehensive suite of regression tests that can be ran against the system under development.

3 Pitfalls of Test Automation As the number of automated test cases grows, so does the cost of maintaining them. A failed test doesn’t always mean a problem with the software. It can mean an assumption made when the test was written is no longer valid. A large comprehensive suite of automated tests acts as a safety net when making changes but it also increases the amount of software that has to be maintained as the system evolves.

4 Two General Approaches to Test Automation Automated GUI testing – using specialized (and expen$ive) software tools that generates keyboard and mouse input and validates the observable behavior of the program is correct. Mercury Interactive (now part of HP) offers automated UI testing software. Automated unit testing – writing routines to test procedures and the public interfaces of classes.

5 Testing Metrics Total number of automated tests. Number of automated test cases that fail during each build. % of failures caused by product errors vs. test case errors (or invalid assumptions). Of the test cases that failed because of an error or now invalid assumption, how many were rewritten vs. thrown away? Time spent creating new automated test cases. Time spent maintaining existing tests.

6 Unit Testing Frameworks Automated unit testing is often done using a framework such as NUnit (.net) or JUnit (Java). To use one of these xUnit frameworks, you write classes that contain methods which exercise the objects and routines in the code you want to test. By following certain conventions as you write these automated unit tests, you can easily manage these unit tests using the NUnit/JUnit/XUnit framework.

7 Example Unit Test Frameworks NUnit for.NET programs JUnit for Java Test::Unit for Ruby PyUnit for Python CppUnit for C++ OCUnit for Objective-C

8 Example Code to test: namespace MyCode { class Arithmetic { public int Add(int i, int j) { return i + j; }

9 NUnit Tests [TestFixture] public class ArithmeticTests { [Test] public void Add_SimpleTest(){ MyCode.Arithmetic a = new MyCode.Arithmetic(); int answer = a.Add(3,7); Assert.AreEqual(10, answer); } [Test] public void Add_BigNumbers() { MyCode.Arithmetic a = new MyCode.Arithmetic(); int answer = a.Add(int.MaxValue, 0); Assert.AreEqual(int.MaxValue, answer); }

10 Output From NUnit

11 Dependency Injection Objects are more flexible and easier to test when they allow their dependencies to be set by clients. The following class is not easy to test. It has a dependency on the file “AboutData.txt”. Testing the class involves making sure a file with the proper format exists on the file system. It can be made easier to test by redesigning it to use Dependency Injection.

12 public class AppDescription { private String name; private String version; private String author; public AppDescription() throws IOException { BufferedReader br = new BufferedReader( new FileReader("AboutData.txt")); name = br.readLine(); version = br.readLine(); author = br.readLine(); br.close(); } public String getAppDescription() { return name + " " + version + " was written by " + author; }

13 public class AppDescription { private String name; private String version; private String author; public AppDescription() throws IOException { this(new FileReader("AboutData.txt")); } public AppDescription(Reader r) throws IOException { BufferedReader br = new BufferedReader(r); name = br.readLine(); version = br.readLine(); author = br.readLine(); br.close(); } public String getAppDescription() { return name + " " + version + " was written by " + author; }

14 Dependency Injection via setter methods The previous example showed DI via a class’s constructor. You can also inject dependencies via setter methods. public class Dependent { private AService s1; private AnotherService s2; public Dependent() { } public setAService(AService s) { s1 = s; } public setAnotherService(AnotherService s) { s2 = s; }

15 Dependency Injection Example See class CourseGateway in SQLLiteExample. There is also a JUnit test for CourseGateway showing how dependency injection can make unit testing easier/possible. URL: https://github.com/burrise/AndroidSampleCo de/tree/master/SQLLiteExample https://github.com/burrise/AndroidSampleCo de/tree/master/SQLLiteExample

16 References

17 Creating and running unit tests with Android Studio There are JUnit extensions for Android to test Android components. Unit tests can be hard to write because application code is often tied to the application’s runtime environment. For example, to create a SQLite DB in android you need the application’s context. This makes it more difficult to write unit tests independent of the application. JUnit extensions for Android solve the problem. – Class android.test.AndroidTestCase provides access to an application’s context. – Class ActivityInstrumentationTestCase2 gives access to the application’s Activity (testing through the GUI)

18 Examples LuckyCount – demonstrates basic unit testing LuckyCount SQLLiteExample - demonstrates dependency injection with unit tests SQLLiteExample TestingThroughTheGUI – demonstrates automated tests that send input through the GUI. TestingThroughTheGUI For more detailed instruction on how to create and run automated unit tests in Android Studio, see: http://sce2.umkc.edu/BIT/burrise/pl/testing/And roidStudioUnitTesting.docx http://sce2.umkc.edu/BIT/burrise/pl/testing/And roidStudioUnitTesting.docx

19 Creating and running tests of Android applications in Eclipse with ADT There are JUnit extensions for Android to test Android components. Unit tests can be hard to write because application code is often tied to the application’s runtime environment. For example, to create a SQLite DB in android you need the application’s context. This makes it more difficult to write unit tests independent of the application. JUnit extensions for Android solve the problem. The JUnit extension class android.test.AndroidTestCase provides access to an application’s context.

20 Automated Android Activity Testing A well-designed program will separate program logic from the user interface. Among other benefits, it makes testing easier. Most program logic can be tested without going through the UI (something that requires physical input), but it does mean the portion code in the view and event handling routines for the view aren’t covered by automated unit tests. Until now! The Android instrumentation framework provides support for writing automated unit tests that send events to the user interface. It’s called Activity testing.

21


Download ppt "Automated Unit Testing. Test Automation Manual testing is laborious and time consuming. Computer automation has transformed many sectors of our economy."

Similar presentations


Ads by Google