Google Professional Cloud Developer Question 271
Select 3Google Cloud PlatformYou are developing a serverless application on Google Cloud Platform using Cloud Functions. The function processes user input and writes data to a Firestore database. You are tasked with writing unit tests for this function. Which practices should you follow to ensure your unit tests are effective and reliable?
- A
Mock external dependencies such as Firestore during testing.
- B
Deploy the function to a test environment and test it against live Firestore data.
- C
Test only the business logic of the function, not the behavior of Firestore.
- D
Use a local emulator to replicate Firestore behavior for integration testing.
- E
Ensure the tests are isolated and do not depend on external services.
Show answer and explanation
Correct answers: A, C, E
Explanation
Unit testing aims to isolate the function being tested and validate its logic without relying on external systems or resources. Mocking dependencies like Firestore and focusing only on the business logic of the function ensure that the tests are fast, reliable, and not brittle. Using live Firestore data or local emulators is more aligned with integration testing and not suitable for unit testing scenarios.
- A. Correct.
Mocking external dependencies, such as Firestore, is a best practice for unit testing as it ensures the test only focuses on the function's logic, not the behavior of external services.
- B. Incorrect.
Deploying the function to a test environment and testing against live Firestore data is not a unit testing practice; this is an integration testing approach and can lead to brittle tests dependent on external systems.
- C. Correct.
Unit tests should focus on testing the business logic of the function and not external systems like Firestore. Testing Firestore behavior would fall under integration testing.
- D. Incorrect.
Using a local emulator for Firestore is more relevant to integration testing, not unit testing. Unit testing should mock external dependencies instead of relying on emulators.
- E. Correct.
Isolating tests from external services ensures that unit tests remain reliable, fast, and focused on the function's internal logic, which is a key aspect of effective unit testing.