简体   繁体   English

如何使用mix运行测试

[英]How to run tests with mix

Update: for anyone arriving at this question from Google, you can run the tests of a given Elixir project by running mix test . 更新:对于从Google收到此问题的任何人,您可以通过运行mix test来运行给定Elixir项目的mix test

As per the docs , Elixir Script Files (with an extension of .exs) do not need to be compiled before they are ran. 根据文档 ,Elixir脚本文件(扩展名为.exs)在运行之前不需要编译。

Here is an exact quote from the Elixir docs: 以下是Elixir文档的确切引用:

the test file is an Elixir script file (.exs). 测试文件是Elixir脚本文件(.exs)。 This is convenient because we don't need to compile test files before running them 这很方便,因为我们不需要在运行它们之前编译测试文件


So I am adding a module file and test file to the faker library here 所以我增加了模块文件和测试文件到图书馆骗子这里

I have already compiled the project by running mix compile in the project root. 我已经通过在项目根目录中运行mix compile项目。

The problem arises when I attempt to run all the test files in addition to my new test file, using mix test . 当我尝试使用mix test运行除了新测试文件之外的所有测试文件时,问题就出现了。 I receive the following error: 我收到以下错误:

** (CompileError) test/faker/shakespeare_test.exs:3: module Faker.Shakespeare is not loaded and could not be found
    (stdlib) lists.erl:1353: :lists.mapfoldl/3
    (stdlib) lists.erl:1354: :lists.mapfoldl/3

The error says that the module couldn't be found, but it's clearly in bin. 错误说无法找到该模块,但它显然在bin中。 What am I doing wrong? 我究竟做错了什么?

The contents of lib/Shakespeare.ex are: lib/Shakespeare.ex的内容是:

defmodule Shakespeare do

  def hamlet_quote do
    Enum.at(hamlet, :crypto.rand_uniform(0, Enum.count(hamlet)))
  end

  def as_you_like_it_quote do
    Enum.at(as_you_like_it, :crypto.rand_uniform(0, Enum.count(as_you_like_it)))
  end

  def king_richard_iii_quote do
    Enum.at(king_richard_iii, :crypto.rand_uniform(0, Enum.count(king_richard_iii)))
  end

  def romeo_and_juliet_quote do
    Enum.at(romeo_and_juliet, :crypto.rand_uniform(0, Enum.count(romeo_and_juliet)))
  end

  def hamlet do
      ["To be, or not to be: that is the question.",
       "Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry.",
       "This above all: to thine own self be true.",
       "Though this be madness, yet there is method in 't.",
       "That it should come to this!.",
       "There is nothing either good or bad, but thinking makes it so.",
       "What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! .",
       "The lady doth protest too much, methinks.",
       "In my mind's eye.",
       "A little more than kin, and less than kind.",
       "The play 's the thing wherein I'll catch the conscience of the king.",
       "And it must follow, as the night the day, thou canst not then be false to any man.",
       "Brevity is the soul of wit.",
       "Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love.",
       "Rich gifts wax poor when givers prove unkind.",
       "Do you think I am easier to be played on than a pipe?",
       "I will speak daggers to her, but use none.",
       "When sorrows come, they come not single spies, but in battalions."]
  end

  def as_you_like_it do
      ["All the world 's a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts.",
       "Can one desire too much of a good thing?.",
       "I like this place and willingly could waste my time in it.",
       "How bitter a thing it is to look into happiness through another man's eyes!",
       "Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude.",
       "True is it that we have seen better days.",
       "For ever and a day.",
       "The fool doth think he is wise, but the wise man knows himself to be a fool."]
  end

  def king_richard_iii do
      ["Now is the winter of our discontent.",
       "A horse! a horse! my kingdom for a horse!.",
       "Conscience is but a word that cowards use, devised at first to keep the strong in awe.",
       "So wise so young, they say, do never live long.",
       "Off with his head!",
       "An honest tale speeds best, being plainly told.",
       "The king's name is a tower of strength.",
       "The world is grown so bad, that wrens make prey where eagles dare not perch."]
  end

  def romeo_and_juliet do
      ["O Romeo, Romeo! wherefore art thou Romeo?.",
       "It is the east, and Juliet is the sun.",
       "Good Night, Good night! Parting is such sweet sorrow, that I shall say good night till it be morrow.",
       "What's in a name? That which we call a rose by any other name would smell as sweet.",
       "Wisely and slow; they stumble that run fast.",
       "Tempt not a desperate man.",
       "For you and I are past our dancing days.",
       "O! she doth teach the torches to burn bright.",
       "It seems she hangs upon the cheek of night like a rich jewel in an Ethiope's ear.",
       "See, how she leans her cheek upon her hand! O that I were a glove upon that hand, that I might touch that cheek!.",
       "Not stepping o'er the bounds of modesty."]
  end

 end

The contents of test/Shakespeare_test.ex is: test/Shakespeare_test.ex的内容是:

defmodule Faker.ShakespeareTest do
  use ExUnit.Case, async: true
  import Faker.Shakespeare

  test :hamlet_quote do
    assert String.length(hamlet_quote) != 0
    assert Regex.match?(~r/.+/, hamlet_quote)
  end

  test :as_you_like_it_quote do
    assert String.length(as_you_like_it_quote) != 0
    assert Regex.match?(~r/.+/, as_you_like_it_quote)
  end

  test :king_richard_iii_quote do
    assert String.length(king_richard_iii_quote) != 0
    assert Regex.match?(~r/.+/, king_richard_iii_quote)
  end

  test :romeo_and_juliet_quote do
    assert String.length(romeo_and_juliet_quote) != 0
    assert Regex.match?(~r/.+/, romeo_and_juliet_quote)
  end
end

You are defining the module (not class) Shakespeare but your tests are using the module Faker.Shakespeare . 您正在定义模块(不是类) Shakespeare但您的测试使用的是模块Faker.Shakespeare

If you define your module name correctly as defmodule Faker.Shakespeare , it should work. 如果您正确地将模块名称定义为defmodule Faker.Shakespeare ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM