Adding new test cases for increasing test coverage of Loklak Server
It is a good practice to have test cases covering major portion of actual code base. The idea was same to add new test cases in Loklak Server to increase its test coverage. The results were quite amazing with a significant increase of about 3% in total test coverage of the overall project. And about 80-100% increase in the test coverage of individual files for which tests have been written.
Integration Process
For integration, a total of 6 new test cases have been written:
- ASCIITest
- GeoLocationTest
- CommonPatternTest
- InstagramProfileScraperTest
- EventBriteCrawlerServiceTest
- LocationWiseTimeServiceTest
For increasing code coverage, Java docs have been written which increase the lines of code being covered.
Implementation
Basic implementation of adding a new test case is same. Here’s is an example of EventBriteCrawlerServiceTest implementation. This can be used as a reference for adding a new test case in the project.
Prerequisite: If the test file being written tests any additional external service (e.g. EventBriteCrawlerServiceTest tests any event being created on EventBrite platform) then, a corresponding new test service or test event should be written beforehand on the given platform. For EventBriteCrawlerServiceTest, a test-event has been created.
The given below steps will be followed for creating a new test case (EventBriteCrawlerServiceTest), assuming the prerequisite step has been done:
- A new java file is generated in test/org/loklak/api/search/ as EventBriteCrawlerServiceTest.java.
- Define package for the test file and import EventBriteCrawlerService.java file which has to be tested along with necessary packages and methods.
package org.loklak.api.search; import org.loklak.api.search.EventBriteCrawlerService; import org.loklak.susi.SusiThought; import org.junit.Test; import org.json.JSONArray; import org.json.JSONObject; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNotNull;
- Define the class and methods that need to be tested. It is a good idea to break the testing into several test methods rather than testing more features in a single method.
public class EventBriteCrawlerServiceTest { @Test public void apiPathTest() { } @Test public void eventBriteEventTest() { } @Test public void eventBriteOrganizerTest() { } @Test public void eventExtraDetailsTest() { } }
- Create an object of EventBriteCrawlerService in each test method.
EventBriteCrawlerService eventBriteCrawlerService = new EventBriteCrawlerService();
- Call specific method of EventBriteCrawlerService that needs to be tested in each of the defined methods of test file and assert the actualresult to the expectedresult.
@Test public void apiPathTest() { EventBriteCrawlerService eventBriteCrawlerService = new EventBriteCrawlerService(); assertEquals("/api/eventbritecrawler.json", eventBriteCrawlerService.getAPIPath()); }
- For methods fetching an actual result (Integration tests) of event page, define and initialise an expected set of results and assert the expected result with the actual result by parsing the json result.
@Test public void eventBriteOrganizerTest() { EventBriteCrawlerService eventBriteCrawlerService = new EventBriteCrawlerService(); String eventTestUrl = "https://www.eventbrite.com/e/ testevent-tickets-46017636991"; SusiThought resultPage = eventBriteCrawlerService .crawlEventBrite(eventTestUrl); JSONArray jsonArray = resultPage.getData(); JSONObject organizer_details = jsonArray.getJSONObject(1); String organizer_contact_info = organizer_details .getString("organizer_contact_info"); String organizer_link = organizer_details .getString("organizer_link"); String organizer_profile_link = organizer_details .getString("organizer_profile_link"); String organizer_name = organizer_details .getString("organizer_name"); assertEquals("https://www.eventbrite.com /e/testevent-tickets-46017636991 #lightbox_contact", organizer_contact_info); assertEquals("https://www.eventbrite.com /e/testevent-tickets-46017636991 #listing-organizer", organizer_link); assertEquals("", organizer_profile_link); assertEquals("aurabh Srivastava", organizer_name); }
- If the test file is testing the harvester, then import and add the test class in TestRunner.java file. e.g.
import org.loklak.harvester.TwitterScraperTest; @RunWith(Suite.class) @Suite.SuiteClasses({ TwitterScraperTest.class })
Testing
The last step will be to check if the test case passes with integration of other tests. The gradle will test the test case along with other tests in the project.
./gradlew build
Resources
- Official Javadocs: junit
- GithubProfileScraperTest: Loklak Server
You must be logged in to post a comment.