Editing
Test-Driven Development in Elixir
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Getting Started with TDD in Elixir === To start practicing TDD in Elixir, you need to follow a few steps: 1. **Install Elixir**: Ensure that you have Elixir installed on your machine. Visit the [https://elixir-lang.org/install.html Elixir Installation Guide] if you haven't set it up yet. 2. **Create a New Elixir Project**: Use the `mix` command to create a new Elixir project. For example, run `mix new my_project` to generate a new project named "my_project". 3. **Add Testing Dependencies**: Open the `mix.exs` file in your project's root directory and add the [`ExUnit`](https://hexdocs.pm/ex_unit/ExUnit.html) testing framework as a dependency. Add the following line to the `deps` function: ```elixir {:ex_unit, "~> 1.12", only: :test} ``` 4. **Write Your First Test**: Create a new test file in the `test` directory with a name ending in `_test.exs`, such as `my_project_test.exs`. Inside the file, define a test module using the `ExUnit.Case` behavior: ```elixir defmodule MyProjectTest do use ExUnit.Case test "example test" do assert 1 + 1 == 2 end end ``` 5. **Run the Tests**: Execute the tests using the `mix test` command. This command will run all the tests in your project and display the results. 6. **Write Production Code**: Write the actual code to make the tests pass. Continuously run the tests after making changes to ensure they pass and don't introduce any regressions. 7. **Refactor**: Once the tests pass, you can refactor your code to improve its design and maintainability. Remember to re-run the tests after each refactoring step to make sure your changes haven't broken anything.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information