Python Forum
Assert that dictionary contains a key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assert that dictionary contains a key
#1
Hi,

I am new to Python and and am trying to learn testing (using unittest).

How do I assert that a dictionary contains a key?

Cheers,

Mick
Reply
#2
You want to assert or just test if the key in dict?
assert some_key in some_dict, which is same as assert some_key in some_dict.keys()?

or in unnittets
assertIn(some_key, some_dict)
assertIn(some_key, some_dict.keys())

https://docs.python.org/3/library/unitte...e.assertIn
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Brilliant, thanks. I suspected it should be that simple.:-)
Reply
#4
just to add - pytest is really nice test suite
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assert failure jtcostel 1 1,651 Sep-03-2021, 05:28 PM
Last Post: buran
  assert Naito 1 2,052 Jan-16-2019, 05:06 PM
Last Post: ODIS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020