mockstar / docs / index.rst

Mockstar -- Mocking Like a Rockstar!

Mockstar is a bunch of small enhances on top of Mock library that can give you a lot of mocking and unit-testing goods.


Mockstar is still in development, but my team already uses it in our project heavily, so it should get stable API soon.

Unit-testing with MockStar

So, you want to implement and test your unit. Let's say it's a function :func:`create_user` that will look like this when it is done:

This unit consists of input-parameters:

  • email
  • password
  • full_name

and seven side-effects:

  • User model
  • not_md5_and_has_salt function
  • count_score function
  • choose_low_quality_avatar function
  • choose_high_quality_avatar function
  • mail business-logic
  • discover_possible_friends function

So, to test this unit in isolation we would need to mock-out all side-effects, on every test put some return-values so that they will fit our if-else clauses, and finally, generate suitable input-parameters.

With Mock library, you would do something like this:

Problems I see:

  • need to repeat mocked names as test parameters
  • need to write autospec=True again and again
  • need to write module prefix on every patch call
  • need to patch on every test case
  • need to add common return_values and assign to some variables (like user) that we'll use later in asserts
  • side_effects take a lot of space in our testing code, I want to separate them

With mockstar your test would look something like this:

I hope you like mockstar's aspiration to fight copy-paste and useless typing.