Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91328 > unrolled thread

assertRaises() help

Started byVincent Davis <vincent@vincentdavis.net>
First post2015-05-27 16:09 -0600
Last post2015-05-28 11:38 +1000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  assertRaises() help Vincent Davis <vincent@vincentdavis.net> - 2015-05-27 16:09 -0600
    Re: assertRaises() help Steven D'Aprano <steve@pearwood.info> - 2015-05-28 11:38 +1000

#91328 — assertRaises() help

FromVincent Davis <vincent@vincentdavis.net>
Date2015-05-27 16:09 -0600
SubjectassertRaises() help
Message-ID<mailman.103.1432764591.5151.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

I am trying to add a test to pandas Int the first case I assert that I get
a NaT value, in the second I what to test that I get a value error.
def
test_day_not_in_month_coerce_true
() works
I am trying to duplicate them with coerce=
False which
will give a ValueError but I cant get the tests to work.


class TestDaysInMonth(tm.TestCase):
    def test_day_not_in_month_coerce_true(self):
        self.assertTrue(isnull(to_datetime('2015-02-29', coerce=True)))
        self.assertTrue(isnull(to_datetime('2015-02-29', format="%Y-%m-%d",
coerce=True)))
        self.assertTrue(isnull(to_datetime('2015-02-32', format="%Y-%m-%d",
coerce=True)))
        self.assertTrue(isnull(to_datetime('2015-04-31', format="%Y-%m-%d",
coerce=True)))
    def test_day_not_in_month_coerce_false(self):
        self.assertRaises(ValueError, to_datetime, '2015-02-29',
coerce=False)

what I get is

FAIL: test_day_not_in_month_coerce_false
(pandas.tests.test_tseries.TestDaysInMonth)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/vmd/GitHub/pandas_vmd/pandas/tests/test_tseries.py", line
747, in test_day_not_in_month_coerce_false
    self.assertRaises(ValueError, to_datetime, '2015-02-29', coerce=False)
  File "/Users/vmd/GitHub/pandas_vmd/pandas/util/testing.py", line 1576, in
assertRaises
    _callable(*args, **kwargs)
  File "/Users/vmd/GitHub/pandas_vmd/pandas/util/testing.py", line 1640, in
__exit__
    raise AssertionError("{0} not raised.".format(name))
AssertionError: ValueError not raised.

>From the docs maybe I should be using a "with" statement​.


Vincent Davis
720-301-3003

[toc] | [next] | [standalone]


#91339

FromSteven D'Aprano <steve@pearwood.info>
Date2015-05-28 11:38 +1000
Message-ID<5566717e$0$13003$c3e8da3$5496439d@news.astraweb.com>
In reply to#91328
On Thu, 28 May 2015 08:09 am, Vincent Davis wrote:

> I am trying to add a test to pandas Int the first case I assert that I get
> a NaT value, in the second I what to test that I get a value error.

Your sentence has just enough typos to lead me to doubt I understand you.
What do you mean, add a test "to pandas Int the first case..." ?

What's a NaT value?


> def
> test_day_not_in_month_coerce_true
> () works

If it works, why is it relevant to your question?


> I am trying to duplicate them with coerce=
> False which
> will give a ValueError but I cant get the tests to work.
[...]

>     def test_day_not_in_month_coerce_false(self):
>         self.assertRaises(ValueError, to_datetime, '2015-02-29',
> coerce=False)


What leads you to believe that to_datetime('2015-02-29', coerce=False) will
raise ValueError?


> what I get is
> 
> FAIL: test_day_not_in_month_coerce_false
> (pandas.tests.test_tseries.TestDaysInMonth)

This failure clearly shows that to_datetime(...) does not raise ValueError.

I don't see anything obviously wrong in your use of the assertRaises method,
*assuming* that it is the assertRaises method from the standard library
unittest module. If it is some custom method written by you, or part of
pandas, then I have no idea if you are doing something wrong.

But I expect that the real problem is that you are mistaken to believe that
to_datetime('2015-02-29', coerce=False) raises ValueError.




-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web