Docsity

Unit Testing and Regression Testing in CSIS 110: An Electronic Diary Example - Prof. Brian, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 – Lecture 34
Announcements
Read section 6.4.
No class Friday
Testing
Last time we started looking at testing. We did some unit testing of a prototype of an
electronic diary.
What is unit testing?
Unit Testing tests smaller pieces of the code (such as a method). Units can be items such
as a class, a group of classes, or even a single method. Unit testing can be performed long
before the application is complete.
What techniques did you use to use when you were unit testing the diary?
- Called methods from BlueJ
- Used object inspector
- Examined return values and text output.
What bugs did you find?
- Array index error when you try to make a 2 hour appointment at 5 PM
- Overwrite problem if you make a 2 hour appointment 1 hour before another
appointment
- Array index error if you try to find an open time for a two hour appointment, and
there is only an opening at 5 PM.
When you fixed those problems, what testing did you do?
Did you go back and run all of your 1-hour tests again? Why or why not?
If not, how do you know that you didn’t break anything that used to work?
Regression Testing:
Experience has shown that a software bugs are frequently introduced when a programmer
fixes a different problem.
So, it is a good idea to run regression tests. Regression testing is the process of re-
running tests that have already passed to ensure that changes to the code didn't introduce
errors in parts of the code that used to work.
pf3
pf4
pf5

Partial preview of the text

Download Unit Testing and Regression Testing in CSIS 110: An Electronic Diary Example - Prof. Brian and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 34

Announcements Read section 6.4. No class Friday Testing Last time we started looking at testing. We did some unit testing of a prototype of an electronic diary. What is unit testing? Unit Testing tests smaller pieces of the code (such as a method). Units can be items such as a class, a group of classes, or even a single method. Unit testing can be performed long before the application is complete. What techniques did you use to use when you were unit testing the diary?

  • Called methods from BlueJ
  • Used object inspector
  • Examined return values and text output. What bugs did you find?
  • Array index error when you try to make a 2 hour appointment at 5 PM
  • Overwrite problem if you make a 2 hour appointment 1 hour before another appointment
  • Array index error if you try to find an open time for a two hour appointment, and there is only an opening at 5 PM. When you fixed those problems, what testing did you do? Did you go back and run all of your 1-hour tests again? Why or why not? If not, how do you know that you didn’t break anything that used to work? Regression Testing: Experience has shown that a software bugs are frequently introduced when a programmer fixes a different problem. So, it is a good idea to run regression tests. Regression testing is the process of re- running tests that have already passed to ensure that changes to the code didn't introduce errors in parts of the code that used to work.

How would you have gone back and run your one-hour tests? Well, if you have to do it by hand, it is tedious and unlikely to get done. Imagine if you were testing something with 10,000 lines instead of 50. So, we want to automate the testing process so it is easier to run regression tests. Let's look at the diary-testing project. Class OneHourTests automates some of the tests that we did earlier. For example, the method makeThreeAppointments makes an appointment at the beginning of the day, at the end of the day, and in the middle of the day. Let's write a TwoHourTests class. What does it need to do? How do we fix some of these problems? OK, so now we can easily rerun all of our tests after we change the code. Is there anyway that this could be better? Well, we still have to examine the results by hand. If there are lots of tests, this will be time consuming and error-prone. Also, note that if there is a java exception, the test stops executing. We can't run all of our tests until we fix the problem. Automated Checking of Test Results It would be nice if we could automate the checking of test results when we do regression testing. BlueJ supports this with the Junit testing framework. Open the diary-testing-junit-v1 project. What's different? Well, first of all there is this funny class DayTest, which is identified as a unit test class. We also need to re-configure BlueJ: Select Tools | Preferences | Miscellaneous. Select the Show unit testing tools option. You should now see some extra buttons below the compile button in the main BlueJ window. Press the Run Tests button. What happened? (Can all select Run All from the right click menu associated with the DayTest class) We can also run just one of the tests. So, where did this class come from? Well, BlueJ created it for us, automatically!

Now we have a test. Let's execute it. Let's look at the source code. Let's write a test that checks to make sure that we can't schedule a 2-hour appointment that begins only 1-hour before a different appointment. What do we need to do? Create a Day object Create an Appointment and Schedule it for 10 AM Create a 2-hour appointment and Schedule it for 9AM Let's write a test for the Appointment class. We want to write a test that makes sure that Appointment objects are initialized correctly when we create them.

Lab 27 Open the triangle-junit project. In this lab, I want you to create some unit tests for the Triangle class. The Triangle class includes:

  • A constructor. The constructor has three parameters, which represent the lengths of the three sides
  • A getTriangleVariety method. This method returns a String, which is supposed to indicate the type of Triangle. The return value is one of the following: o "equilateral" if all sides have the same length. o "isosceles" if two sides have the same length and the third is different. o "scalene" if all sides have different lengths. o "right triangle" if the sides form a right triangle. o "not a triangle" if the lengths do not form a triangle. Write some unit tests to see if the getTriangleVariety method works correctly. Use the homework submission system to turn in TriangleTest.java. This is Lab 27. Lab 27 Open the triangle-junit project. In this lab, I want you to create some unit tests for the Triangle class. The Triangle class includes:
  • A constructor. The constructor has three parameters, which represent the lengths of the three sides
  • A getTriangleVariety method. This method returns a String, which is supposed to indicate the type of Triangle. The return value is one of the following: o "equilateral" if all sides have the same length. o "isosceles" if two sides have the same length and the third is different. o "scalene" if all sides have different lengths. o "right triangle" if the sides form a right triangle. o "not a triangle" if the lengths do not form a triangle. Write some unit tests to see if the getTriangleVariety method works correctly. Use the homework submission system to turn in TriangleTest.java. This is Lab 27.