CSE116的学习笔记-Lec#3:Scala Unit Testing
Unit Testing
Testing
- How do you know if your code is correct?
- Submit to AutoLab?
- Does not exist outside of class
- Does not exist for your project
Recall
1 | package example |
- How do we test this function to verufy that it’s correct?
1 | package example |
- Call the method from main
- Print the results
- Manaually verify
What about Large Projects?
There may be 100’s of files and 1000’s of methods
Any change in a function might break any code
Will you manually verify all that code for each change?
Unit Testing
- Automate testing
- Provide structure to testing
Unit Testing
- Run a serious of tests on your code
- If the code is correct, all test should pass
- If the code is incorrect, at least one test should fail
- A set of test should test every possible error that could occur
1 | package tests |
- Use Maven to download scalatest
- Click Maven in the IntelliJ sidebar to interact with pom.xml
Create a new test that will be executed when this file is ran
No main method
FunSuite controls execution instead of main
Is this enough testing?
1 | package tests |
Use data structure to run many test cases
Unit Testing Objectives
- Each homework, and other places in the course, will have objectives that require through testing
- When these objectives are graded, your test suite is ran:
- Against your solution
- Against a correct solution stored on the server
- Against a variety of incorrect solution stored on the server
- Your test suite should pass on both your solution and the correct solution
- Your test suite should fail on all the incorrect solutions
Maven: Dependency Management
- To run this testing code, we used an external library named Scalatest
- Scalatest does not come with Scala
- We must download it before running tests
- To manage external libraies, we’ll use Maven
- List all dependancies(libraies)in a file named pom.xml
- Save pom.xml in the root directory of your project
- Use Maven to download all dependancies
- The pom.xml is similar to the requirements.txt file we used in Python
Lecture Question
Method: In a package named “lecture” create an object named “FirstObject” with a metho named “computeShippingCost” that takes a Double representing the weight of a package as a paramater and returns a Double representing the shipping cost of the package.
The shipping cost is ($)5 + 0.25 for each pound over 30.
Unit Testing: In a package named “tests” create a class / file named “UnitTesting” as a test suite that tests the computeShipping Cost method.