Client Test m1 V1a PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

JPMorgan Chase Software Engineering Virtual Experience

Task 1 - software
engineering bonus task!
Module 1 - Interface with a stock price data feed
and create tests for it
Bonus task: `client_test.py`

● Once you've applied your changes in the client application, you now have
to ensure you cover them with enough unit tests.

● Unit tests are useful in software development for a bunch of reasons:


● It helps increase developers’ confidence in the code they write
● It helps us to design better robust / maintainable / reusable code
● It helps us document how our code works for other developers to use.
● It helps us catch errors in code modifications early on

● Unit tests have nothing to do with having a running client application. They
run independently from the main application and exist for the reasons
stated above. The test cases we feed into the methods we test for
represent the use cases we anticipate for those methods
● In the case of this task, the methods we want to test are getDataPoint and
getRatio. We want to cover these with tests so that we ensure these
methods are returning the correct output as individual methods thereby
ensuring us that when we use them together in the main client application,
things will work fine.

● In unit testing, we always follow the pattern, Build-Act-Assert:


○ Build: We first build a simulated test scenario e.g. instantiating the
dummy data we will pass in the methods we’ll test, importing the class
whose methods we want to test, etc.
○ Act: We then make some operations and call the method we want to test
for
○ Assert: We check if the output of the method we’re testing matches the
expectation we have (e.g. dummy / static data of the outcome)
● Unit tests mostly use static / dummy data to represent the test cases we pass
into methods and the expected outcome of these methods when such test
cases are given. This is fine because we want our methods and tests to be
deterministic and not randomly failing / behaving in a different way we don’t
expect.

● For this part of the task, you atleast have to add assertions to the test methods
that already exist in the `client_test.py` file.

● An assertion would look something like the following bullet points below:
○ self.assertEqual(getDataPoint(quote),dataPoint)
○ self.assertEqual(1,1)

● For more examples on assertions in python unit testing check this resource or
other online resource about Python unit testing
● As a rule of thumb, aim to use only one assertion. Use the strongest
assertion you can think of. For instance, in this case, having multiple
asserts to the output of getDataPoint isn’t advisable. Instead, since the
getDataPoint returns a tuple, assert that it returns a tuple containing the
data you expect.

● In addition you are not limited to adding assertions…

● In fact, if you wish you can modify the test functions entirely in
client_test.py

● As a tip to further improve the unit tests, consider adding more tests.
Only getDataPoint is covered so far so try to cover for getRatio as well
e.g. when you have to compute for the ratio and you have the case
where price B is zero, or when price A is zero, etc.
More Tips:

● To help get you started, for the initial tests that exist in the client_test.py
file but are half complete, all you have to do is to add assertions to
them, nothing more.

● The quotes is a list of individual quotes that you can pass into the
getDataPoint function. Each quote represents a possible variation of
quote data but if getDataPoint was modified properly, then it should
return a tuple that contains (stock, top_bid_price, top_ask_price,
stock_price_based_on_formula).. That’s what you will have to assert for
for each quote you pass in. (meaning you’ll have to iterate quotes list
and make assertions per iteration)

● Check the next slide on how you want the end result to be
● Notice how the two tests have the same assertion. So what’s the diff?

● The only difference is that the quotes list in the second test always contains quotes
whose top_bid_price is greater than the top_ask_price. Hence the name of the test
calculatePriceBidGreaterThanAsk, (where bid_price is greater than ask_price)

● Technically nothing is stopping you from just removing the second test and copying
each quote in its quotes list into the quotes list of the first test
Other tips:

● For those that used the python3 version of this repo make sure you’re importing the
client file i.e. client3.py

note: The image above imports specific functions/methods in client3.py i.e. getDataPoint and getRatio. If
you want to import the entire client3.py you can simply just put from client3 import *

You might also like