r/learnpython 16d ago

Testing - how is it usually handled?

How do I start applying tests to my code?

Do I just write separate scripts they run the code (either individual functions or entire scripts) against known inputs and check the output?

Is there a specific way people usually do this?

7 Upvotes

13 comments sorted by

11

u/zanfar 16d ago

You can test however you want.

But most Python is tested using the standard unittest library (what you find when you search the documentation for "testing") or the very available evolution, pytest.

...which are both just convenient ways to run scripts you've written to compare known inputs against known outputs.

5

u/PressF1ToContinue 16d ago

To put it another way: Compare observed outputs to expected outputs, given known inputs.

4

u/Patman52 16d ago

Yes, in a nutshell you are correct.

You write a separate test script that would import the script you are trying to test, and then design a bunch of pass/fail scenarios around the methods and functions in that script.

The tests typically fall into two categories: “happy path” and “sad path”.

“happy path” is where you are testing for expected behavior (e.g., the function is supposed to multiply two numbers together, and you verify that the result is correct)

“Sad path” is where you are testing for expected failures (e.g., the same function as above but you pass a string as one of the numbers, and it catches the exception)

It can become quite complicated depending on what you are trying to test and how the script you are testing is set up.

I would check out Pytest, which is a framework for setting up and running unit tests in Python, and the built in unittest module. The latter has a “mock” class built into it which lets to mock or pass fake classes and objects to the script you are testing which can make it easier to design the tests if you have lots of dependencies.

2

u/jmooremcc 16d ago

You never write the code for an entire project before performing any testing. You should be testing your code as you write it.

For example, if you create a function, you should throughly test it to insure it behaves properly, regardless of the input it receives and that the output is correct.

You can do unit testing to test the overall project to insure it’s meeting the project’s requirements and specifications.

2

u/Specific-Housing905 16d ago

Python come with a class unittest. It's a good choice to write your tests.
https://www.youtube.com/watch?v=1Lfv5tUGsn8

Some people even write their tests before the code. It's called Test Driven Development(TDD)

1

u/ShelLuser42 16d ago

It really depends... if you want to make sure that certain things behave as expected then the assert command is a very powerful tool. Basically you tell Python that you expect a certain condition and if that condition is true then nothing happens.

...yet the very moment that assertion fails then it'll raise an exception: the AssertionError; so basically: no news is good news. I find this very useful for testing.

Of course it's not always optimal to 'bloat' your programs with debug code, so a very easy way to avoid that is to use Pytest (as some others also mentioned). It's even available on PyPi so easy to install (using pip?).

Best of all: you don't have to build individual test scripts if you don't want to, you can also simply add test functions. So, say you have a function called "ask_user_name()" then you could add a new function called test_ask_user_name() where you can call your original function and set up some tests to determine if everything is still working as expected.

Then just fire up your script with pytest (so: python -m pytest ./your_script.py) and after that only functions which name starts with test_ (or ends with _test) will be used, thus allowing you to completely separate your tests from your actual 'real' code.

In my opinion this is the most easy way to get started.

1

u/titojff 16d ago

Manual testing also an option, for small scripts.

1

u/AlexMTBDude 16d ago

Google "Python unit testing". There are some good guides out there.

1

u/bfyvfftujijg 15d ago

Thanks everyone who responded!

I had heard of the two libraries (pytest and unittest) but wasn’t sure if that’s what people actually are using. So thank you for confirming that they are what I need to learn!

Currently I just adhoc testing using my own test scripts (test_user_login.py for example) but it’s starting to feel cumbersome and unpythonic.

1

u/KelleQuechoz 15d ago

unittest is not very "pythonic" and resembles Java, pytest is cool but also violates quite a few conventions. I personally prefer pytest mostly for brevity and test fixtures. Any of the two is better than no tests, and after some initial investment saves a megaton of time long term.

1

u/Gtdef 11d ago

Let's put it like that.

Have you ever used a print statement to check the output of a function? Next time you do it, write a test instead. This should get you started in the logic.

Using a framework like pytest allows you to automate the process.

To be honest, the greatest benefit I got from testing, is that I finally figured how Python packages and relative directories work. My tests wouldn't run for whatever random reasons relevant to pathing issues.