3 Lessons from Working with Open Source Projects

Maybe you’ve wondered how some processes work while using a Open Source tool or a library.

Here are three facts that will help you understand Open Source projects better using Chrome, Flask, and JUnit as examples.

During Nearsoft’s Academy there is a phase in which we have to take a look at Open Source (OS) projects and understand what they are about. The goal is to read code from other people and fix issues or make contributions.

In my case, I chose Chrome, Flask and JUnit. These brought up questions for me and here I describe what I learned.

How Does Chrome Hide Ads?

Chrome blocks a set of different ads, not all of them like an extension would do (e.g. AdBlock). It does block ads that do not conform to the Coalition for Better Ads guidelines.

For example,

Using EasyList, Google gets these different rules and blocks ads from showing. Though Google uses its own modified version of those rules, it doesn’t mean that their own ads like AdSense will not be blocked should a rule violation occur.

The way an ad blocker works is by injecting a stylesheet to the web page. This contains selectors that are set to display:none !important, thus hiding the ads.

Before I started, I had no idea how an ad blocker worked. It turned to be this simple. The key are the rules in EasyList, as simple as it may sound.

The EasyList editorial team decides what to include, or not, in the list. Suggestions come from the project’s very active community.

How Can You Debug an App in Flask?

There are different ways to run your app in debug mode using Flask. You can use any of these options,

  • app.debug = True
  • app.run(debug = True)
  • FLASK_DEBUG = 1 flask run

You can use the latter if you are running from a terminal.

In all cases, this will open an interactive traceback on the console in the browser if there are errors.
At the end of the main module, you could add these lines,

if __name__ == "__main__":
   app.run(debug = True)

On an error, you’ll see something like the image below,

This will make it easier to find what is causing your app to fail.

Before I started, I didn’t know how to hone in on bugs. Debugging was a “try-to-imagine-what-is-causing-it” task.

Now I learned that with these simple commands I can find those bugs quicker, outside of an IDE.

What’s the Order of Execution of Tests in JUnit?

In JUnit, by design, tests cases are run in no particular order. Until now, the methods used were invoked in the order returned by the reflection API. But the Java platform does not specify any particular order. In fact, JDK 7 returns a random order.

Now, from version 4.11, it is possible to use a deterministic order (MethodSorters.DEFAULT). This can be changed by annotating the test class, and then specifying one of the methods. For example,

@FixMethodOrder(MethodSorters.JVM)
This is the order returned by the JVM. It may vary from run to run.
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
Sorts test methods by method name, in lexicographic order.

Here’s an example.

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestMethodOrder {

    @Test
    public void testA() {
        System.out.println("first");
    }
    @Test
    public void testB() {
        System.out.println("second");
    }
    @Test
    public void testC() {
        System.out.println("third");
    }
}

These tests would execute printing first, second, and third, in that order, according to the test method names (testA(), testB(), and testC()).

Before I started, the order of execution of tests was not something that bothered me. Then I realized that there are tests that you should run in a specific order to know what and how things are working.

Now I learned that order is important, it keeps tests organized and makes it clear to understand unit testing better.

What Are Parameterized Tests in JUnit?
There is a custom runner for JUnit that implements parameterized tests. These tests allow the developer to run the same test over and over, using different values.
To create a parameterized test, you have to,

  • Annotate the test class with @RunWith(Parameterized.class).
  • Create a public static method annotated with @parameters that returns a Collection of Objects (as Array) as test data set.
  • Create an instance variable for each “column” of test data.
  • Create your test case using the instance variables as the source of the test data.
import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {     
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }  
           });
    }

    private int fInput;

    private int fExpected;

    public FibonacciTest(int input, int expected) {
        this.fInput = input;
        this.fExpected = expected;
    }

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}

public class Fibonacci {
    public static int compute(int n) {
    	int result = 0;
    	
        if (n <= 1) { 
        	result = n; 
        } else { 
        	result = compute(n - 1) + compute(n - 2); 
        }
        
        return result;
    }
}

Before I started, I thought these testing methods had to be filled one by one, even the repetitive tests with the same data.

Now I learned that this is a way of automating this. It avoids unnecessary work and reduces errors.

Resources

Focus Mode

Contact Request

Close

We will call you right away. All information is kept private