mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-05 12:40:22 +03:00
Create a new module for tests and write their setup code
This commit is contained in:
parent
9f72bd8ca3
commit
89567ccd11
85
readthedocs/developing/tests.rst
Normal file
85
readthedocs/developing/tests.rst
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
=====
|
||||||
|
Tests
|
||||||
|
=====
|
||||||
|
|
||||||
|
In order to test Telegram, the library makes use of the public test servers.
|
||||||
|
|
||||||
|
To run the tests, you need to provide the test runner with two environment
|
||||||
|
variables, containing the string-sessions for both a user account and a bot
|
||||||
|
account.
|
||||||
|
|
||||||
|
These accounts will talk to each other in order to run all the tests involving
|
||||||
|
high level functions.
|
||||||
|
|
||||||
|
|
||||||
|
Dependencies
|
||||||
|
============
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
pip install pytest pytest-asyncio --user
|
||||||
|
|
||||||
|
|
||||||
|
Generating Sessions
|
||||||
|
===================
|
||||||
|
|
||||||
|
First you must login with your phone number to the test servers. It is not
|
||||||
|
possible to use a public phone number because anyone could interrupt the
|
||||||
|
tests while running, and they are not allowed to create bots.
|
||||||
|
|
||||||
|
With the following code, you will authorize a new user session in the public
|
||||||
|
test servers, and also create a bot with a randomly-generated username. Once
|
||||||
|
both are created, it will print a string session of both.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
|
from telethon import TelegramClient
|
||||||
|
from telethon.sessions import StringSession
|
||||||
|
|
||||||
|
# From https://my.telegram.org
|
||||||
|
API = 123, 'abc...'
|
||||||
|
DC = 2, '149.154.167.40', 443
|
||||||
|
PHONE = '+34...'
|
||||||
|
|
||||||
|
# Generate a random username for the bot account (24 chars long is good)
|
||||||
|
bot_username = ''.join(random.choice(string.ascii_lowercase) for _ in range(21)) + 'bot'
|
||||||
|
|
||||||
|
client = TelegramClient(StringSession(), *API)
|
||||||
|
client.session.set_dc(*DC)
|
||||||
|
|
||||||
|
# You will receive a SMS with the code
|
||||||
|
with client.start(phone=PHONE):
|
||||||
|
# Print your client session as a variable
|
||||||
|
print('CLIENT_SESSION="{}"'.format(client.session.save()))
|
||||||
|
|
||||||
|
# Now, create a new bot to also get a bot session
|
||||||
|
with client.conversation('BotFather') as conv:
|
||||||
|
conv.send_message('/newbot')
|
||||||
|
conv.get_response()
|
||||||
|
conv.send_message('Telethon Test Bot')
|
||||||
|
conv.get_response()
|
||||||
|
conv.send_message(bot_username)
|
||||||
|
token = conv.get_response().get_entities_text(types.MessageEntityCode)[0][1]
|
||||||
|
|
||||||
|
bot = TelegramClient(StringSession(), *API)
|
||||||
|
bot.session.set_dc(*DC)
|
||||||
|
with bot.start(bot_token=token):
|
||||||
|
print('BOT_SESSION="{}"'.format(bot.session.save()))
|
||||||
|
|
||||||
|
|
||||||
|
Running the Tests
|
||||||
|
=================
|
||||||
|
|
||||||
|
Put the generated environment variables in your environment, and then run:
|
||||||
|
|
||||||
|
.. code-block:: sh
|
||||||
|
|
||||||
|
pytest
|
||||||
|
|
||||||
|
On the root directory of the project. If everything went well, you should be
|
||||||
|
greeted with all tests passing. If they don't pass, please `open an issue`_.
|
||||||
|
|
||||||
|
.. _open an issue: https://github.com/LonamiWebs/Telethon/issues
|
|
@ -92,6 +92,7 @@ You can also use the menu on the left to quickly skip over sections.
|
||||||
developing/philosophy.rst
|
developing/philosophy.rst
|
||||||
developing/test-servers.rst
|
developing/test-servers.rst
|
||||||
developing/project-structure.rst
|
developing/project-structure.rst
|
||||||
|
developing/tests.rst
|
||||||
developing/coding-style.rst
|
developing/coding-style.rst
|
||||||
developing/understanding-the-type-language.rst
|
developing/understanding-the-type-language.rst
|
||||||
developing/tips-for-porting-the-project.rst
|
developing/tips-for-porting-the-project.rst
|
||||||
|
|
3
setup.py
3
setup.py
|
@ -218,7 +218,8 @@ def main():
|
||||||
install_requires=['pyaes', 'rsa'],
|
install_requires=['pyaes', 'rsa'],
|
||||||
extras_require={
|
extras_require={
|
||||||
'cryptg': ['cryptg']
|
'cryptg': ['cryptg']
|
||||||
}
|
},
|
||||||
|
tests_require=['pytest', 'pytest-asyncio']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
10
tests/client/test_base.py
Normal file
10
tests/client/test_base.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from telethon import types
|
||||||
|
|
||||||
|
pytestmark = pytest.mark.asyncio
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_me(client):
|
||||||
|
me = await client.get_me()
|
||||||
|
assert isinstance(me, types.User)
|
36
tests/conftest.py
Normal file
36
tests/conftest.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
pytestmark = pytest.mark.asyncio
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def bot(request):
|
||||||
|
import os
|
||||||
|
from telethon import TelegramClient
|
||||||
|
from telethon.sessions import StringSession
|
||||||
|
cl = TelegramClient(StringSession(os.environ['BOT_SESSION']), 1, '-')
|
||||||
|
|
||||||
|
def fin():
|
||||||
|
cl.disconnect() # note: this runs the loop
|
||||||
|
|
||||||
|
request.addfinalizer(fin)
|
||||||
|
|
||||||
|
await cl.start()
|
||||||
|
return cl
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def client(request):
|
||||||
|
import os
|
||||||
|
from telethon import TelegramClient
|
||||||
|
from telethon.sessions import StringSession
|
||||||
|
cl = TelegramClient(StringSession(os.environ['CLIENT_SESSION']), 1, '-')
|
||||||
|
|
||||||
|
def fin():
|
||||||
|
cl.disconnect() # note: this runs the loop
|
||||||
|
|
||||||
|
request.addfinalizer(fin)
|
||||||
|
|
||||||
|
await cl.start()
|
||||||
|
return cl
|
Loading…
Reference in New Issue
Block a user