Skip to content

Latest commit

 

History

History
130 lines (98 loc) · 4.75 KB

Testing.md

File metadata and controls

130 lines (98 loc) · 4.75 KB

Testing

🔶 1. Do you write tests? What are the types of tests?


Answer:

Yes, I am familiar with writing tests. There are several types of tests, including: Unit tests: tests individual units of code, such as a specific function or method, in isolation. Integration tests: tests how different units of code work together. Functional tests: tests the functionality of a system as a whole by simulating user interactions. Performance tests: tests how a system performs under a certain load or stress. Acceptance tests: tests whether a system meets the requirements of the stakeholders.

🔶 2. What is mock? How to use it?


Answer:

A mock is a replacement for an object or a function that allows you to control its behavior during a test. This is useful when you have a dependency on an external service or a resource that is not available during the test. In Python, the unittest.mock library provides the MagicMock class for creating mocks.

Example:

from unittest.mock import MagicMock

def my_function(dependency):
    return dependency.do_something()

# Create a mock object
mock_dependency = MagicMock()

# Test the function
my_function(mock_dependency)

# Assert that the mock's method was called
mock_dependency.do_something.assert_called()

🔶 3. How to test a function that depends on system time?


Answer:

To test a function that depends on system time, you can use the unittest.mock library to replace the datetime module with a mock. You can then control the current time returned by the mock.

from unittest.mock import MagicMock, patch

def my_function():
    from datetime import datetime
    return datetime.now().hour

# Test the function
with patch('datetime.datetime') as mock_datetime:
    mock_datetime.now.return_value = datetime(year=2020, month=1, day=1, hour=10)
    assert my_function() == 10

🔶 4. How to check that mock was called once and with the right set of parameters?


Answer:

You can use the assert_called_once() and assert_called_with() methods provided by the MagicMock class to check if a mock was called with the expected parameters.

Example:

mock_dependency.do_something.assert_called_once()
mock_dependency.do_something.assert_called_with('arg1', 'arg2')

🔶 5. How to test an async function?


Answer:

To test an async function, you can use the asyncio.run() method to run the function as a coroutine, or use the unittest.TestCase.loop context manager provided by the asynctest library.

Example:

import asyncio

async def my_async_function():
    await asyncio.sleep(1)
    return 'Hello World'

# Using asyncio.run
assert asyncio.run(my_async_function()) == 'Hello World'

# Using unittest.TestCase.loop
import unittest
from asynctest import TestCase

class MyTestCase(TestCase):
    async def test_my_async_function(self):
        result = await my_async_function()
        self.assertEqual(result, 'Hello World')

🔶 6. How to test code that runs in a thread or process?


Answer:

To test code that runs in a thread or process, you can use the unittest.TestCase.run_in_executor() method to run the function in a thread or process, and use the concurrent.futures library to create a thread or process pool.

Example:

import concurrent.futures
import unittest

def my_function():
    return 'Hello World'

class MyTestCase(unittest.TestCase):
    def test_my_function(self):
        with concurrent.futures.ThreadPoolExecutor() as executor:
            future = executor.submit(my_function)
            self.assertEqual(future.result(), 'Hello World')

        with concurrent.futures.ProcessPoolExecutor() as executor:
            future = executor.submit(my_function)
            self.assertEqual(future.result(), 'Hello World') 

🔶 7. How to test a web application?


Answer:

To test a web application in Python, there are several popular libraries and frameworks that can be used. Some of the common options include:

  • unittest and requests: The built-in unittest library along with the requests library can be used to send HTTP requests to the application and test the responses.
  • pytest and pytest-flask: pytest is a popular testing framework that can be used to write test cases for a web application built with the Flask framework.
  • Django test client: The built-in django.test.Client class can be used to simulate client interactions and test the views, forms, and templates for an application built using the Django framework.
  • WebTest, Selenium: these are other libraries that can be used for testing web applications in python.
  • It's important to use the right tools and libraries depending on the framework, platform and the complexity of the application.