Welcome! I know we've met before but remembering names can be difficult when meeting so many new people,
so again my name is Matthew Berger
and I am Senior Software Engineer at Hyphen. It's good to see you again.
Today we'll go over a few questions about code comprehension and testing over the course of about 30 minutes. If you're ready to start, let's dive in!
Given the following code, answer the questions below.
function add(numbers: string): number {
let integers = numbers.split(',').map(x => parseInt(x));
let negatives = integers.filter(x => x < 0);
if (negatives.length > 0)
throw new RangeError('Negatives are not allowed: ' + negatives.join(', '));
return integers
.filter(x => x <= 1000)
.reduce((a, b) => a + b, 0);
}
let result = add('1, 2, 4, 5');
console.log(result);
-
What does this code do?
- Takes in a string of numbers separated by commas and sums them up. Throws an error if a negative number is detected
-
What kind of test would be good to test this functionality and why?
- Unit test, small isolated function that can be given input and have its output easily compared. No network calls or dependencies to mock.
-
What might that test look like?
- Take a string of numbers as input, run the method, and compare expected output
Given the following code, answer the questions below.
import time
def compute(x):
response = expensive_api_call()
return response + x
def expensive_api_call():
time.sleep(1000) # takes 1,000 seconds to run
return 123
def test_compute():
expected = 124
actual = compute(1)
assert expected == actual
-
What does the test in this code do?
- The test in this code makes an expensive API call that takes 1,000 seconds to run before returning the sum of
123
and the inputx
.
- The test in this code makes an expensive API call that takes 1,000 seconds to run before returning the sum of
-
What is the problem with the test and how could it be resolved?
- The test calls an expensive api call, which burdens the test suite unnecessarily. This can be avoided by
mock
ing the expensive api call, in order to test thecompute
functionality as intended. This test will be instant!
- The test calls an expensive api call, which burdens the test suite unnecessarily. This can be avoided by
-
Assume the code was kept as is. If it relies on a call to an API available on the same machine that must be present for the test to run, what would you call this kind of test?
- An integration test.
-
Assume the code was kept as is. Would this test be a better candidate for running on a local machine or in a continuous integration pipeline and why?
- This test would be a better candidate for running in a continuous integration pipeline because it is a long running test.
// This is using the `Cypress` framework
describe('Find author at articles.com', () => {
beforeEach(() => {
cy.visit('/');
});
it('Find the author Steve Austin', () => {
cy.get('#js-search-input').type('Steve Austin');
cy.get('h2 > a').first().click();
cy.get('.author-post__author-title').click();
cy.contains('.author__title', 'Steve Austin').should('be.visible');
});
});
-
What does the test in this code do?
- The test in this code ensures that
articles.com
is open, then finds an author by the name ofSteve Austin
by filling a search field with the textSteve Austin
. Then it navigates to the author's first article and clicks it. Afterward, it opens the author's page by clicking on the author title in the post, and does a final check whether we are on the author's site by ensuring the desired author's name is visible in the title.
- The test in this code ensures that
-
What kind of test is this considered?
- An end to end test