Skip to content

Commit 38987cc

Browse files
committed
add files
1 parent 597f3af commit 38987cc

File tree

17 files changed

+423
-0
lines changed

17 files changed

+423
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div align="center">
2+
<h1>Unittest</h1>
3+
</div>
4+
5+
<div align="justify">
6+
7+
This repository contains a set of Python unit tests for testing the functionality of the code. Unit tests are automatically executed test scripts that check the correct operation of certain parts of the code, such as functions, classes, modules, and other components.
8+
9+
<br>
10+
11+
The main features of the repository:
12+
- A set of diverse unit tests that cover various aspects of the functionality of the code
13+
- Using Python's standard unittest module to write and run unit tests
14+
- Examples of using the unittest.mock library to create mock objects and simulate the behavior of dependencies in tests
15+
- Coverage of various types of testing such as positive testing, negative testing, exception testing and other scenarios
16+
- Clear organization of tests into structured test classes and methods for easy navigation and maintenance of the test suite
17+
- etc.
18+
19+
<br>
20+
21+
The purpose of this repository is to provide examples of Python unit tests that you can use to test your code and ensure it works correctly.
22+
23+
</div>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import unittest
2+
import warnings
3+
4+
5+
class MyTests(unittest.TestCase):
6+
def test_assert_equal(self):
7+
self.assertEqual(2 + 2, 4) # Check for equality
8+
9+
def test_assert_not_equal(self):
10+
self.assertNotEqual(2 + 2, 5) # Check for inequality
11+
12+
def test_assert_true(self):
13+
self.assertTrue(2 + 2 == 4) # Truth check
14+
15+
def test_assert_false(self):
16+
self.assertFalse(2 + 2 == 5) # False check
17+
18+
def test_assert_is(self):
19+
a = [1, 2, 3]
20+
b = a
21+
self.assertIs(a, b) # Check for identity of objects
22+
23+
def test_assert_is_not(self):
24+
a = [1, 2, 3]
25+
b = [1, 2, 3]
26+
self.assertIsNot(a, b) # Check for non-identity of objects
27+
28+
def test_assert_in(self):
29+
a = [1, 2, 3]
30+
self.assertIn(2, a) # Check for the presence of an element in the sequence
31+
32+
def test_assert_not_in(self):
33+
a = [1, 2, 3]
34+
self.assertNotIn(4, a) # Check for the absence of an element in the sequence
35+
36+
def test_assert_is_none(self):
37+
a = None
38+
self.assertIsNone(a) # Check for equality with None
39+
40+
def test_assert_is_not_none(self):
41+
a = 42
42+
self.assertIsNotNone(a) # Test for inequality with None
43+
44+
def test_assert_almost_equal(self):
45+
self.assertAlmostEqual(0.1 + 0.2, 0.3, delta=0.00001) # Check for approximate equality of numbers with a given delta
46+
47+
def test_assert_not_almost_equal(self):
48+
self.assertNotAlmostEqual(0.1 + 0.2, 0.3, places=20) # delta=0.00001 or places=20 Check for non-approximate equality of numbers with a given delta
49+
50+
def test_assert_raises(self):
51+
with self.assertRaises(ValueError): # Check for a specific exception
52+
int("abc")
53+
54+
def test_assert_warns(self):
55+
with self.assertWarns(DeprecationWarning):
56+
warnings.warn("This function is deprecated", DeprecationWarning) # Check for a specific warning
57+
58+
def test_assert_regex(self):
59+
self.assertRegex("hello world", r'hello') # Check for regular expression match
60+
61+
def test_assert_not_regex(self):
62+
self.assertNotRegex("hello world", r'goodbye') # Check for non-matching regular expression
63+
64+
65+
if __name__ == '__main__':
66+
unittest.main()

test 01 country/Lib/__init__.py

Whitespace-only changes.

test 01 country/Lib/Сountry.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def get_country(iso_code=None):
2+
country_dict = {
3+
'US': {
4+
'name': 'United States of America'
5+
},
6+
'CA': {
7+
'name': 'Canada'
8+
},
9+
'UK': {
10+
'name': 'United Kingdom'
11+
},
12+
'DE': {
13+
'name': 'Germany'
14+
},
15+
'JP': {
16+
'name': 'Japan'
17+
},
18+
'AU' : {
19+
'name' : 'Australia'
20+
}
21+
}
22+
23+
if type(iso_code) != str:
24+
raise TypeError('iso_code need to be a string')
25+
elif len(iso_code) != 2:
26+
raise ValueError('iso_code need to be 2 characters long')
27+
28+
if iso_code in country_dict:
29+
return True, country_dict[iso_code]
30+
else:
31+
return False, None

test 01 country/testСountry.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
from Lib.Сountry import get_country
3+
4+
5+
class TestLibraryСountry(unittest.TestCase):
6+
def test_allow_country(self):
7+
# Testing allowed countries
8+
iso_code_test_list = ['US', 'CA', 'UK', 'DE', 'JP', 'AU']
9+
for iso_code in iso_code_test_list:
10+
country = get_country(iso_code=iso_code)
11+
self.assertTrue(country[0]) # Check if the first element of the tuple is True
12+
self.assertEqual(dict, type(country[1])) # Check if the second element of the tuple is a dictionary
13+
14+
def test_disallow_country(self):
15+
# Testing a banned country
16+
country = get_country(iso_code='BR')
17+
self.assertFalse(country[0]) # Check if the first element of the tuple is False
18+
self.assertEqual(None, country[1]) # Check if the second element of the tuple is None
19+
20+
def test_raise_country_TypeError(self):
21+
# Testing for throwing a TypeError exception when the function is used incorrectly
22+
with self.assertRaises(TypeError):
23+
get_country() # Throw TypeError without passing arguments
24+
get_country(iso_code=12) # Throw a TypeError with a numeric argument
25+
26+
def test_raise_country_ValueError(self):
27+
# Testing for throwing a ValueError exception when passing an invalid iso_code argument value
28+
with self.assertRaises(ValueError):
29+
get_country(iso_code='Japan') # Throw a ValueError with an invalid iso_code value
30+
get_country(iso_code='J') # Throw a ValueError with a short iso_code value
31+
get_country(iso_code='') # Throw a ValueError passing an empty string as the value of iso_code

test 02 person/Lib/Person.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import datetime
2+
3+
4+
class Person:
5+
def allowed_to_buy_alcohol(self, birthday, alcohol_percentage):
6+
# age 16-18: < 16.5%
7+
# age 18 and over: > 16.5%
8+
age = self.calculate_age(birthday)
9+
return age >= 18 and alcohol_percentage > 16.5
10+
11+
def allowed_to_buy_tobacco(self, birthday):
12+
# age 18 and over
13+
age = self.calculate_age(birthday)
14+
return age >= 18
15+
16+
def calculate_age(self, birthday):
17+
today = datetime.date.today()
18+
birthdate = datetime.datetime.strptime(birthday, '%Y-%m-%d').date()
19+
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
20+
return age

test 02 person/Lib/__init__.py

Whitespace-only changes.

test 02 person/testPerson.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import unittest
2+
from Lib.Person import Person
3+
4+
5+
class TestAllowedToBuyAlcohol(unittest.TestCase):
6+
def setUp(self):
7+
# Set up the test by creating a Person object
8+
self.__person = Person()
9+
10+
def tearDown(self):
11+
# Clean up the test by deleting the Person object
12+
del self.__person
13+
14+
def test_age_are_too_low_to_buy(self):
15+
# Test case to check if a person with age below the legal limit cannot buy alcohol
16+
result = self.__person.allowed_to_buy_alcohol('2008-01-01', 4.6)
17+
self.assertFalse(result)
18+
print("Test result: Sale is not allowed. (age: too low)")
19+
20+
def test_age_is_allowed_to_buy(self):
21+
# Test case to check if a person with age above the legal limit can buy alcohol
22+
result = self.__person.allowed_to_buy_alcohol('1980-01-01', 46.6)
23+
self.assertTrue(result)
24+
print("Test result: Sale is allowed. (age: allowed)")
25+
26+
def test_age_is_allowed_to_buy_tobacco(self):
27+
# Test case to check if a person with age above the legal limit can buy tobacco
28+
result = self.__person.allowed_to_buy_tobacco('2000-01-01')
29+
self.assertTrue(result)
30+
print("Test result: Sale is allowed. (age: allowed)")
31+
32+
def test_age_is_not_allowed_to_buy_tobacco(self):
33+
# Test case to check if a person with age below the legal limit cannot buy tobacco
34+
result = self.__person.allowed_to_buy_tobacco('2015-01-01')
35+
self.assertFalse(result)
36+
print("Test result: Sale is not allowed. (age: too low)")
37+
38+
39+
if __name__ == '__main__':
40+
unittest.main()

test 03 discount/Lib/Discount.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Discount:
2+
def __init__(self, discount_rate):
3+
self.discount_rate = discount_rate
4+
self.discounted_categories = ['phone', 'laptop', 'tv']
5+
6+
def apply_discount(self, price, category):
7+
if category in self.discounted_categories:
8+
return price * (1 - self.discount_rate)
9+
else:
10+
return price

test 03 discount/Lib/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)