junction icon indicating copy to clipboard operation
junction copied to clipboard

Improve code coverage

Open ChillarAnand opened this issue 9 years ago • 11 comments

we need to write unit tests!

ChillarAnand avatar Apr 27 '15 14:04 ChillarAnand

+1. Slowly keep adding it, not at one shoot :-)

kracekumar avatar Apr 27 '15 20:04 kracekumar

+1

theskumar avatar May 02 '15 06:05 theskumar

I can write tests. Is there a specific way I have to write them? I usually put them in the application folder.

theSage21 avatar May 16 '15 18:05 theSage21

Yes, it is good to have unit_tests directory inside the app directory. Unit tests shouldn't connect to db, mocks/stubs are used with help of mock library. Use py.test as test runner and plain asserts.

Writing unit test is super fun when the view layer is split into multiple function where each function does one thing. While writing unit tests please feel free to add extra layer/refactor the functions.

Though the title says about unit tests, you should also write functional tests starting from view to response.

Start with something simple and we can add more as we proceed.

kracekumar avatar May 16 '15 18:05 kracekumar

I am having trouble running the existing tests. py.test fails with internal error. Is there a specific way to run tests?

theSage21 avatar Jul 06 '15 10:07 theSage21

Show the output ?

On Mon, Jul 6, 2015 at 3:57 PM, arjoonn sharma [email protected] wrote:

I am having trouble running the existing tests. py.test fails with internal error. Is there a specific way to run tests?

— Reply to this email directly or view it on GitHub https://github.com/pythonindia/junction/issues/219#issuecomment-118803380 .

Regards Kracekumar Ramaraju http://kracekumar.com +91 85530 29521

kracekumar avatar Jul 06 '15 11:07 kracekumar

Aah, sorry, it was my mistake. I was mistakenly running from ~/dev/junction as opposed to ~/dev/junction/junction. Realized this immediately after I commented.

theSage21 avatar Jul 06 '15 11:07 theSage21

Is this how I should proceed? https://github.com/theSage21/junction/commit/265f053b2d9945973302bcf21a8fc1ab1801366b

theSage21 avatar Jul 11 '15 09:07 theSage21

You don't need to mock email functionality, checkout outbox module[1].

With pytest, you can also use available outbox fixture[2], while in development django is set to use consoleBackend so not actual email sent out anyways.

Sample Test:

def test_some_function(outbox):
  some_function()
  assert len(outbox) == 1
  assert "sometext" in outbox

Footnotes:

[1] https://docs.djangoproject.com/en/1.8/topics/testing/tools/#email-services [2] https://github.com/pythonindia/junction/blob/master/tests/fixtures.py#L49-L53

theskumar avatar Jul 13 '15 03:07 theskumar

@palnabarun @pradyunsg How much code coverage should be good ? We can split up up the task per module.

ananyo2012 avatar Mar 20 '20 19:03 ananyo2012

@ananyo2012 We should at least cover all important functionalities.

The coverage percentage metric is misleading. Lower coverage certainly means more tests are required to be written, but higher coverage does not lead to well-tested software.

palnabarun avatar Mar 20 '20 19:03 palnabarun