MCQs on Testing in Elixir | Elixir

Elixir provides powerful tools for testing, with its built-in framework ExUnit. Understanding unit tests, assertions, process testing, and mocking/stubbing is essential for writing robust and maintainable code. This guide explores how to effectively implement testing in Elixir, helping you ensure your applications work correctly in all scenarios.


1. Writing Unit Tests with ExUnit

  1. What is the default testing framework in Elixir?
    • a) ExUnit
    • b) Minitest
    • c) RSpec
    • d) JUnit
  2. How do you define a test in ExUnit?
    • a) test "description" do
    • b) def test "description"
    • c) describe "description" do
    • d) test_case "description" do
  3. Which function is used to run all the tests in ExUnit?
    • a) ExUnit.run/0
    • b) ExUnit.run_all/0
    • c) ExUnit.start/0
    • d) ExUnit.execute/0
  4. Which function is used to ensure that the test process terminates after running the test case in ExUnit?
    • a) ExUnit.stop/0
    • b) ExUnit.end/0
    • c) ExUnit.terminate/0
    • d) ExUnit.cleanup/0
  5. What does the assert function do in ExUnit?
    • a) It checks if a condition is true
    • b) It skips the test if the condition fails
    • c) It logs test execution details
    • d) It runs the test asynchronously
  6. What is the purpose of the setup block in ExUnit?
    • a) To initialize state or prepare resources before running tests
    • b) To define test assertions
    • c) To tear down resources after tests
    • d) To skip certain tests based on conditions
  7. How do you mark a test as asynchronous in ExUnit?
    • a) By using async: true
    • b) By defining async before the test block
    • c) By calling ExUnit.async/1
    • d) By placing the test in a separate file
  8. How do you run a specific test case in ExUnit?
    • a) mix test <test_case>
    • b) mix test --only <test_case>
    • c) mix test --name <test_case>
    • d) mix test <test_case_name>
  9. How do you handle failures in ExUnit tests?
    • a) By using assert to check if the expected output is true
    • b) By writing error messages manually
    • c) By returning :ok for passed tests
    • d) By using assert_fail to handle failures
  10. Which module provides functionality for testing in Elixir?
    • a) ExUnit
    • b) ExTest
    • c) TestElixir
    • d) ElixirTest

2. Assertions and Test Setup

  1. What is the primary use of the assert keyword in ExUnit tests?
    • a) To check if a value is true
    • b) To validate multiple conditions at once
    • c) To compare the expected and actual values
    • d) To run assertions after the test finishes
  2. What does the refute function do in ExUnit?
    • a) Ensures that a condition is false
    • b) Ensures that the condition is true
    • c) Ensures that a value is nil
    • d) Skips the current test case
  3. Which of these functions is used to verify that two values are equal in ExUnit?
    • a) assert_equal/2
    • b) assert_equal/3
    • c) assert/2
    • d) assert_same/2
  4. What happens when an assertion fails in ExUnit?
    • a) The test is marked as failed and an error is reported
    • b) The test continues running
    • c) The system shuts down
    • d) The test is skipped automatically
  5. How do you set up and tear down test data in ExUnit?
    • a) By using the setup and teardown blocks
    • b) By using before and after hooks
    • c) By using start and stop functions
    • d) By manually writing setup and teardown code
  6. What does the setup_all function do in ExUnit?
    • a) Runs before any test case is executed
    • b) Runs before each individual test case
    • c) Runs after each test case finishes
    • d) Skips the entire test suite
  7. In ExUnit, what should you return from the setup block to provide test data to tests?
    • a) { :ok, value }
    • b) { :ok, :ok }
    • c) { value, :ok }
    • d) { value, nil }
  8. How can you test the execution time of a function in ExUnit?
    • a) By using assert_time
    • b) By using assert_speed
    • c) By using assert_in_delta with time values
    • d) By using benchmark method
  9. How do you mock a function call in ExUnit?
    • a) By using Mox library
    • b) By using Mock module
    • c) By using ExMock
    • d) By using Fake function
  10. What type of testing is best done using assertions in ExUnit?
    • a) Unit testing for specific functions or methods
    • b) End-to-end testing
    • c) Integration testing
    • d) Load testing

3. Testing Processes and Concurrency

  1. Which function is used to test if processes are working concurrently in Elixir?
    • a) ExUnit.Process
    • b) spawn/1
    • c) start_link/1
    • d) async_test/1
  2. How do you test the asynchronous behavior of a process in ExUnit?
    • a) By using assert_receive to check for messages
    • b) By using assert_async to test asynchronously
    • c) By using process_test
    • d) By using test_async
  3. What is the purpose of send in testing processes in ExUnit?
    • a) To send a message to a process for asynchronous communication
    • b) To log messages during testing
    • c) To start a new test case
    • d) To send data for assertions
  4. How do you simulate a process crash in ExUnit for testing?
    • a) By using exit/1 or Process.exit/2
    • b) By using throw/1
    • c) By using raise/1
    • d) By using crash/1
  5. How do you receive messages from a process during a test in Elixir?
    • a) receive block
    • b) get_message function
    • c) get_message_from_process
    • d) process_receive
  6. What should you do to ensure that a test does not depend on other tests in ExUnit?
    • a) Use process isolation and avoid shared state
    • b) Use global variables
    • c) Use global resources for each test
    • d) Share state across tests
  7. What is the expected behavior when testing processes with assert_receive in ExUnit?
    • a) The test will pass if the process sends a message within the given time frame
    • b) The test will fail if the process sends a message
    • c) The test will pass if the message is ignored
    • d) The test will crash if a message is sent
  8. What does spawn/1 do in Elixir?
    • a) It creates a new process asynchronously
    • b) It logs a message asynchronously
    • c) It synchronously creates a process
    • d) It sends a message to a process
  9. How do you ensure that processes execute in order during testing in ExUnit?
    • a) By using assert_order in the test block
    • b) By controlling message passing and order in the test
    • c) By using sequential test cases
    • d) By controlling the process scheduler manually
  10. What would assert_receive expect when used in a test for a process?
    • a) A message from a process within the expected time
    • b) A message from any process at any time
    • c) A process crash message
    • d) A process exit message

4. Mocking and Stubbing in Tests

  1. What is the primary use of mocking in tests?
    • a) To simulate functions or behaviors for testing purposes
    • b) To handle process communication
    • c) To run tests concurrently
    • d) To manage database connections
  2. Which library is used for mocking in Elixir tests?
    • a) Mox
    • b) Mockito
    • c) ExMock
    • d) Fake
  3. What does stubbing mean in the context of Elixir testing?
    • a) Replacing function calls with predefined results
    • b) Starting a process asynchronously
    • c) Handling real-time communication between processes
    • d) Setting up initial data for tests
  4. What is a common problem when using mocks in Elixir tests?
    • a) They may cause the test to run slower
    • b) They introduce errors into production code
    • c) They can make tests more complex and harder to maintain
    • d) They are not supported by ExUnit
  5. How do you verify that a mocked function was called in Elixir tests?
    • a) By using expect in the Mox library
    • b) By using assert_called in ExUnit
    • c) By checking logs
    • d) By using verify in the test case

Answers

QnoAnswer
1a) ExUnit
2a) test "description" do
3a) ExUnit.run/0
4a) ExUnit.stop/0
5a) It checks if a condition is true
6a) To initialize state or prepare resources before running tests
7a) By using async: true
8b) mix test --only <test_case>
9a) By using assert to check if the expected output is true
10a) ExUnit
11c) To compare the expected and actual values
12a) Ensures that a condition is false
13c) assert/2
14a) The test is marked as failed and an error is reported
15a) By using the setup and teardown blocks
16a) Runs before any test case is executed
17a) { :ok, value }
18c) By using assert_in_delta with time values
19a) By using Mox library
20a) Unit testing for specific functions or methods
21b) spawn/1
22a) By using assert_receive to check for messages
23a) To send a message to a process for asynchronous communication
24a) By using exit/1 or Process.exit/2
25a) receive block
26a) Use process isolation and avoid shared state
27a) The test will pass if the process sends a message within the given time frame
28a) It creates a new process asynchronously
29b) By controlling message passing and order in the test
30a) A message from a process within the expected time
31a) To simulate functions or behaviors for testing purposes
32a) Mox
33a) Replacing function calls with predefined results
34c) They can make tests more complex and harder to maintain
35a) By using expect in the Mox library

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top