Welcome to your complete adventure in creating, testing, and publishing Julia modules! This guide combines everything you need to know about building professional Julia packages.
- Creating Your First Module
- Package Structure
- Project Configuration
- Testing Your Package
- Advanced Topics
- Common Issues & Solutions
# src/MyAwesomeGame.jl
module MyAwesomeGame
# 1. Import statements
using DataFrames, Statistics
# 2. Include sub-modules
include("types.jl")
include("functions.jl")
# 3. Export public interfaces
export Player, create_player,
punch, kick
end
# src/types.jl
struct Player
name::String
health::Int
strength::Int
end
# src/functions.jl
function create_player(name::String)
if isempty(name)
throw(ArgumentError("Player name cannot be empty"))
end
return Player(name, 100, 10)
end
function punch()
return "POW! ๐"
end
function kick()
return "WHAM! ๐ฆถ"
end
Create your package structure like this:
MyAwesomeGame/
โโโ Project.toml
โโโ LICENSE
โโโ README.md
โโโ src/
โ โโโ MyAwesomeGame.jl
โ โโโ types.jl
โ โโโ functions.jl
โโโ test/
โโโ runtests.jl
โโโ types_tests.jl
โโโ functions_tests.jl
name = "MyAwesomeGame"
uuid = "12345678-1234-5678-1234-567812345678"
authors = ["Your Name <your.email@example.com>"]
version = "0.1.0"
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
[compat]
julia = "1.6"
DataFrames = "1.0"
# In Julia REPL
using UUIDs
uuid4() # Copy this UUID to your Project.toml
using Test
using MyAwesomeGame
# Include all test files
include("types_tests.jl")
include("functions_tests.jl")
@testset "MyAwesomeGame.jl" begin
@testset "Basic Functionality" begin
@test punch() == "POW! ๐"
@test kick() == "WHAM! ๐ฆถ"
end
end
@testset "Player Types" begin
# Test player creation
player = create_player("TestHero")
@test player.name == "TestHero"
@test player.health == 100
@test player.strength == 10
# Test invalid creation
@test_throws ArgumentError create_player("")
end
@testset "Game Functions" begin
@test punch() == "POW! ๐"
@test kick() == "WHAM! ๐ฆถ"
# Test player functions
player = create_player("TestHero")
@test player.name == "TestHero"
end
-
Create New Package
# In Julia REPL ] generate MyAwesomeGame
-
Develop Package
# In Julia REPL ] dev /path/to/MyAwesomeGame
-
Test Package
] test MyAwesomeGame
-
Group Related Tests
@testset "Feature Group" begin @testset "Subfeature 1" begin # Tests end @testset "Subfeature 2" begin # Tests end end
-
Test Different Cases
@testset "Input Validation" begin # Normal cases @test valid_function(valid_input) == expected_output # Edge cases @test valid_function(0) == 0 # Error cases @test_throws ErrorType invalid_function(bad_input) end
-
Test Floating Point Comparisons
@test isapprox(compute_float(), expected, rtol=1e-10)
# Add your package to LOAD_PATH
push!(LOAD_PATH, "@.")
using MyAwesomeGame
Add to Project.toml:
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
# In Julia REPL
] resolve
# Package commands (in REPL)
] generate PackageName # Create new package
] dev PackageName # Develop package
] test PackageName # Run tests
] add PackageName # Install package
# Testing macros
@test # Basic assertion
@test_throws # Error testing
@test_warn # Warning testing
@test_logs # Log testing
@test_broken # Known failing tests
@test_skip # Skip tests
- Project.toml configured
- UUID generated
- Dependencies listed
- Source files organized
- Tests written
- Documentation added
- README updated
- LICENSE added
Feel free to submit issues and enhancement requests!
This project is licensed under the MIT License - see the LICENSE file for details.
Happy coding! May your tests be green and your builds be clean! ๐