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