DEV Community

mohaned mashaly
mohaned mashaly

Posted on

unit-testing

unit testing is defined as testing a function or class or a module in a program to check for any bugs before further testing is carried by the software testing team, a lot of start-ups and companies now expect developers to know unit testing methodologies and frameworks in the back-end language they are using to make the testing process done by software testers afterwards more efficient in terms of time and cost, so it's favourable for a developer/programmer to know unit teasing to ease the life of software tester and make sure his/her code works well to a certain extent, personally I tried unit testing in python, Golang in projects I was working on and somehow familiar with it in java but never used it, I will talk about pytest as a framework for testing and how it promotes a cleaner and more efficient use than the standard testing library in python, but first I will talk about some definition and jargons in pytest

@pytest.fixture : to put it simply fixture is a code which is repeated many times in the code or a variable which is used by many methods/functions i.e: database connection or creating object.

@pytest.mark.module1_test : markers is used to specify which functions you're interested in testing when starting the testing process, for example every function with module1_test signature will be tested.

let's look at a function which test a function which check wether a number is even or not

@pytest.mark.even_test
def is_even():
number1 = 2
assert number1 % 2 == 0

the test will run successfully if the number if the number1 is event else it will fails.

make sure as a developer to add unit testing on to learn list because it's a very powerful tool which can promote good coding habits

Fun to Read :-
https://yeti.co/blog/using-pytest-to-write-beautiful-tests-and-a-bulletproof-django-app/

http://guru99.com/pytest-tutorial.html#3

Top comments (0)