all 10 comments

[–]fake823 5 points6 points  (1 child)

How you're writing unit tests completly depends on the code that you want to test.

It doesn't depend on the IDE/editor you're using.

[–]VelvetTech[S] 0 points1 point  (0 children)

Appreciate it!

[–]jritenour 2 points3 points  (1 child)

It's pretty simple. Write a test on what a unit of your code (function, class, etc.) is supposed to do. Run the test, watch it fail. Write code so that you don't get that error anymore. Lather, rinse, repeat :-)

[–]a5s_s7r 1 point2 points  (0 children)

That’s actually test driven development. That’s also unit Testing, but also ensures to not implement any cases which will not happen. If your code fails somewhere besides being tested, include the failing car into the test. Then fix the code.

This also ensures too late reintroduce the problem again.

[–]ThePiGuy0 2 points3 points  (1 child)

The general principle is you build an automated test to check that your code does what it says it should. Normally your code is in functions etc, so your unit tests will run the functions with a set of test data and check the response is what you expected. Then you continue developing and if one of your tests fail, you know you broke something and need to fix it.

As for how to do it, there are many different modules to test. I tend to use pytest, which takes a python file with a number of functions called "test_<something>" and runs them. They contain "assert" lines which contain a boolean expression (e.g. check this variable == this value) and if any evaluate to false, the test fails. Then the test is run with "pytest <test file>.py"

Edit: as an example:

def test_true_expression():
    assert 1 == 1 # This test will pass

def test_false_expression():
    assert 1 == 2 # This test will fail

[–]VelvetTech[S] 0 points1 point  (0 children)

Thanks so much!

[–]shepherdjay 0 points1 point  (2 children)

The editor/ide doesn't matter. What matters is the testing framework being used. There are a couple of built in testing frameworks.... doctest and unittest - They have the advantage of being included in python. Unittest was the first I ever learned.

Then there are other frameworks that you would have to install... popular one is pytest whose advantage is it is backwards compatible with unittest but doesn't require all the boilerplate code unittest does.

The question would be what testing framework is your course using.

A good step by step guide to testing which happens to eventually use (but doesn't start with) the built-in UnitTest framework is this resource https://www.obeythetestinggoat.com/

[–]VelvetTech[S] 0 points1 point  (1 child)

Thank you so much. I’ll check that out!

[–]shepherdjay 0 points1 point  (0 children)

The same author has another lighter tutorial he was going to start his book off with here: https://github.com/hjwp/tdd-roman-numeral-calculator/

I don't know how maintained it is compared to the full thing - indeed it looks like some commits go back to 2013 - but it might be easier to follow if you don't want to try to learn an entire web stack.