faker-ruby / faker

A library for generating fake data such as names, addresses, and phone numbers.
MIT License
11.18k stars 3.18k forks source link

Add option to silence deprecation warnings when running the tests #2948

Open stefannibrasil opened 2 months ago

stefannibrasil commented 2 months ago

We have recently introduced an internal deprecator for faker. See #2858 and #2919 for more details.

We'd like to have the option to skip printing out the deprecation warnings when running the tests. Currently, we get a long test output due to them.

Ruby's Deprecate gem has the option to wrap a test assertion in a skip_during block, intended to be used in the test suite only:

class TestSomething < Gem::Testcase
  def test_some_thing_with_deprecations
    Gem::Deprecate.skip_during do
      actual_stdout, actual_stderr = capture_output do
        Gem.something_deprecated
      end
      assert_empty actual_stdout
      assert_equal(expected, actual_stderr)
    end
  end
end

It would be nice if we could have a similar option for our custom deprecator so our test output is not filled with those deprecation messages while they are being removed.

keshavbiswa commented 2 months ago

Let me try it.

stefannibrasil commented 2 months ago

awesome, thank you!

keshavbiswa commented 1 month ago

So there are two approaches I see here,

  1. To use class accessors to capture stdouts only when the variable is true, (skip_during will yield and ensure it's set to false after)? This makes it easy to turn it on/off for individual tests, depending on what we want to assert?
  2. The other is to globally disable in in test_helper.rb, this is an easier approach, but makes it difficult to enable/disable individual tests.

@stefannibrasil Lmk what you think.

stefannibrasil commented 1 month ago

hi @keshavbiswa thanks for working on this. Yeah, number 1 seems to be what we need. We want to be able to skip the output when explicitly adding a skip block. If we add the block is because we know the test will generate the warning but there's nothing we can do until the generator is removed/updated, if that makes sense.