Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌐 Add Korean translation for docs/ko/docs/benchmarks.md and docs/ko/docs/tutorial/testing.md #9496

Closed
wants to merge 8 commits into from
34 changes: 34 additions & 0 deletions docs/ko/docs/benchmarks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 벤치마크

독립된 TechEmpower 벤치마크에서 Uvicorn에서 작동하는 **FastAPI**가 <a href="https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=zijzen-7" class="external-link" target="_blank">가장 빠른 파이썬 프레임워크 중 하나</a>로 Starlette과 Uvicorn(FastAPI에서 내부적으로 사용) 바로 다음에 있다고 보여주고 있습니다. (*)

하지만 벤치마크를 확인과 비교 하실때 여러분은 다음 사항을 기억 하셔야 합니다.

## 벤치마크와 속도

벤치마크를 확인할때 서로 다른 유형의 도구들을 동등한 것으로 비교 되는것이 일반적입니다.

특히 Uvicorn, Starlette, 그리고 FastPAI (다른 많은 도구들 중에서)가 함께 비교됩니다.

도구가 해결하는 문제가 단순할수록 더 나은 성능을 얻을 수 있습니다. 그리고 대부분의 벤치마크는 도구가 제공하는 추가 기능을 테스트 하지는 않습니다.

계층 구조는 다음과 같습니다:

* **Uvicorn**: ASGI 서버
* **Starlette**: (Uvicorn 사용) 웹 마이크로 프레임워크
* **FastAPI**: (Starlette 사용) API 구축, 데이터 유효성 검사등 추가 기능이 있는 API 마이크로 프레임워크

* **Uvicorn**:
* 서버 자체를 제외하고는 추가적인 코드가 많지 않음으로 최고의 성능을 발휘합니다.
* 여러분이 Uvicorn으로 직접 애플리케이션을 작성 하지는 않으실겁니다. 즉 여러분의 코드는 최소한 Starlette (또는 **FastAPI**)에서 제공되는 모든 코드를 포함해야 합니다. 만약 여러분이 그렇게 하신다면 여러분의 최종 애플리케이션은 프레임워크를 사용하고 애플리케이션 코드와 버그를 최소화 한것과 동일한 고정경비를 가지게 됩니다.
* 여러분이 Uvicorn을 비교하시려면, Daphne, Hypercorn, uWSGI 등과 같은 애플리케이션 서버와 비교하십시오.
* **Starlette**:
* Uvicorn 다음으로 최고의 성능을 발휘합니다. 실제고 Starlette은 Uvicorn을 사용하여 실행됩니다. 따라서 더 많은 코드를 실행해야 하기 때문에 Uvicorn보다 느릴 수 밖에 없습니다.
* 그렇지만 경로를 기반으로 하는 라우팅등 간단한 웹 애플리케이션을 구축 할 수 있는 도구들를 제공합니다.
* 여러분이 Starlette을 비교하시려면, Sanic, Flask, Django 등과 같은 웹 프레임워크 (또는 마이크로 프레임워크)와 비교하십시오.
* **FastAPI**:
* The same way that Starlette uses Uvicorn and cannot be faster than it, **FastAPI** uses Starlette, so it cannot be faster than it.
* FastAPI provides more features on top of Starlette. Features that you almost always need when building APIs, like data validation and serialization. And by using it, you get automatic documentation for free (the automatic documentation doesn't even add overhead to running applications, it is generated on startup).
* If you didn't use FastAPI and used Starlette directly (or another tool, like Sanic, Flask, Responder, etc) you would have to implement all the data validation and serialization yourself. So, your final application would still have the same overhead as if it was built using FastAPI. And in many cases, this data validation and serialization is the biggest amount of code written in applications.
* So, by using FastAPI you are saving development time, bugs, lines of code, and you would probably get the same performance (or better) you would if you didn't use it (as you would have to implement it all in your code).
* If you are comparing FastAPI, compare it against a web application framework (or set of tools) that provides data validation, serialization and documentation, like Flask-apispec, NestJS, Molten, etc. Frameworks with integrated automatic data validation, serialization and documentation.
212 changes: 212 additions & 0 deletions docs/ko/docs/tutorial/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
# 테스트

<a href="https://www.starlette.io/testclient/" class="external-link" target="_blank">Starlette</a> 덕분에 **FastAPI** 애플리케이션을 테스트 하는 것은 쉽고 재미있습니다.

Requests를 바탕으로 설계된 <a href="https://www.python-httpx.org" class="external-link" target="_blank">HTTPX</a>를 기반으로 하므로 매우 익숙하고 직관적입니다.

이것을 토대로 여러분은 <a href="https://docs.pytest.org/" class="external-link" target="_blank">pytest</a>를 **FastAPI**와 함께 직접 사용하실 수 있습니다.

## `TestClient` 사용하기

!!! info "정보"
`TestClient`를 이용하시려면 먼저 <a href="https://www.python-httpx.org" class="external-link" target="_blank">`httpx`</a>를 설치 하셔야 합니다.

예시) `pip install httpx`.

`TestClient`를 임포트 합니다.

여러분의 **FastAPI** 애플리케이션을 전달 하셔서 `TestClient`를 생성합니다.

`test_`로 시작하는 함수를 생성합니다 (이것은 `pytest`의 표준적인 관례입니다).

`httpx`를 사용하시는 것과 같은 방식으로 `TestClient` 객체를 사용합니다.

여러분이 확인하고 싶으신 항목을 표준 파이썬 수식을 이용해 간단하게 `assert` 문장으로 작성합니다 (다시 한번, `pytest`의 표준적인 관례입니다).

```Python hl_lines="2 12 15-18"
{!../../../docs_src/app_testing/tutorial001.py!}
```

!!! tip "팁"
테스트 함수들은 `async def`가 아닌 일반적인 `def`으로 시작한다는 것에 주목하시기를 바랍니다.

클라이언트를 호출할 때도 `await`이 아닌 일반적인 호출입니다.

이것은 여러분이 복잡한 절차 없이 `pytest`를 바로 사용하실 수 있게 도와줍니다.

!!! note "기술 세부사항"
여러분은 또한 `from starlette.testclient import TestClient`를 사용하실 수도 있습니다.

개발자인 여러분의 편의를 위해 **FastAPI**는 `starlette.testclient`과 동일한 `fastapi.testclient`를 제공합니다. 그러나 이것은 Starlette에서 직접 파생된 기능입니다.

!!! tip "팁"
여러분이 만약 FastAPI 애플리케이션에 요청을 보내는 것과는 별개로 테스트 내에서 `async` 함수를 호출하고 싶으시다면 (예를 들어 비동기적인 데이터베이스 함수) 심화 자습서에 있는 [Async Tests](../advanced/async-tests.md){.internal-link target=_blank}를 참고하시기를 바랍니다.

## 테스트 분리하기

실제 애플리케이션에서 여러분들은 아마 다른 파일에 테스트를 따로 작성하실 것입니다.

그리고 여러분의 **FastAPI** 애플리케이션은 여러 개의 파일과 모듈 등으로 구성이 되어 있을 수도 있습니다.

### **FastAPI** app 파일

여러분이 [Bigger Applications](./bigger-applications.md){.internal-link target=_blank}에 서술된 파일 구조를 가지고 있다고 가정합니다:

```
.
├── app
│   ├── __init__.py
│   └── main.py
```

`main.py`파일 안에 여러분의 **FastAPI** app 이 있습니다:


```Python
{!../../../docs_src/app_testing/main.py!}
```

### 테스트 파일

그렇다면 여러분은 `test_main.py` 파일에 테스트를 작성하실 수 있습니다. 이 파일은 동일한 파이썬 패키지에 위치할 수도 있습니다 (`__init__.py` 파일과 동일한 디렉터리):

``` hl_lines="5"
.
├── app
│   ├── __init__.py
│   ├── main.py
│   └── test_main.py
```

이 파일이 동일한 패키지에 있기 때문에 여러분은 상대적 임포트를 통해 `main`모듈 (`main.py`)에서 `app` 객체를 임포트하실 수 있습니다.

```Python hl_lines="3"
{!../../../docs_src/app_testing/test_main.py!}
```

...그리고 전과 동일하게 테스트 코드를 포함할 수 있습니다.

## 테스트: 확장 예시

이제 이 예시를 확장 시키고 어떻게 다른 부분들을 테스트하는지 살펴보기 위해 추가적인 세부 사항을 더해 봅니다.

### 확장된 **FastAPI** app 파일

앞에서 다룬 파일 구조를 가지고 진행합니다.

```
.
├── app
│   ├── __init__.py
│   ├── main.py
│   └── test_main.py
```

이제 여러분의 **FastAPI** 애플리케이션에 있는 `main.py` 파일에 다른 **path operations**가 있다고 가정해 봅니다.

파일에 에러를 리턴할 소지가 있는 `GET` 연산이 있습니다.

파일에 여러 가지 에러를 리턴할 소지가 있는 `POST` 연산이 있습니다.

두 *path operations* 모두 `X-Token` 헤더가 필요합니다.

=== "Python 3.10+"

```Python
{!> ../../../docs_src/app_testing/app_b_an_py310/main.py!}
```

=== "Python 3.9+"

```Python
{!> ../../../docs_src/app_testing/app_b_an_py39/main.py!}
```

=== "Python 3.6+"

```Python
{!> ../../../docs_src/app_testing/app_b_an/main.py!}
```

=== "Python 3.10+ non-Annotated"

!!! tip "팁"
가능하다면 `Annotated` 버전을 사용하는 것을 선호합니다.

```Python
{!> ../../../docs_src/app_testing/app_b_py310/main.py!}
```

=== "Python 3.6+ non-Annotated"

!!! tip "팁"
가능하다면 `Annotated` 버전을 사용하는 것을 선호합니다.

```Python
{!> ../../../docs_src/app_testing/app_b/main.py!}
```

### 확장된 테스트 파일

이제 여러분은 `test_main.py`에 확장된 테스트를 추가해 업데이트하실 수 있습니다:

```Python
{!> ../../../docs_src/app_testing/app_b/test_main.py!}
```

여러분이 클라이언트가 정보를 요청(request)에 포함해 보내야 하지만 어떻게 해야 하는지 모르실 때 `httpx`에서는 어떻게 하는지 또는 HTTPX 디자인의 바탕이 된 `requests`서는 어떻게 하는지 검색(구글)을 통해 찾아보실 수 있습니다.

그다음에 여러분의 테스트에서 그대로 따라 하시면 됩니다.

예시:

* *path* 또는 *query* 매개변수를 전달하시려면 URL에 직접 대입하시면 됩니다.
* JSON 본문(body)을 전달하시려면 파이썬 객체(예시로 `dict`)를 매개변수 `json`에 대입하시면 됩니다.
* JSON 대신 *Form Data*를 보내시려면 `data` 매개변수를 사용하시면 됩니다.
* *headers*를 전달 하시려면 `headers` 매개변수에 `dict`를 대입해서 하시면 됩니다.
* *cookies*는 `dict`를 `cookies` 매개변수에 대입하시면 됩니다.

`httpx`나 `TestClient`를 이용하여 백엔드로 데이터를 전송하는 방법에 관한 더 많은 정보는<a href="https://www.python-httpx.org" class="external-link" target="_blank">HTTPX documentation</a>에서 찾아보실 수 있습니다.

!!! info "정보"
`TestClient`는 Pydantic 모델이 아니라 JSON으로 변경이 가능한 데이터를 받는다는 점을 숙지 하시기를 바랍니다.

만약 여러분의 테스트에 Pydantic 모델이 있고 테스트 중에 그것의 데이터를 애플리케이션으로 전송하고자 하신다면 [JSON Compatible Encoder](encoder.md){.internal-link target=_blank}에 나와 있는 `jsonable_encoder`를 사용하실 수 있습니다.

## 실행

이후 여러분은 `pytest`를 설치 하시면 됩니다:

<div class="termy">

```console
$ pip install pytest

---> 100%
```

</div>

파일과 테스트들이 자동으로 감지되며 실행이 되고 여러분에게 결과를 보고 합니다.

테스트를 다음과 같이 실행하십시오:

<div class="termy">

```console
$ pytest

================ test session starts ================
platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /home/user/code/superawesome-cli/app
plugins: forked-1.1.3, xdist-1.31.0, cov-2.8.1
collected 6 items

---> 100%

test_main.py <span style="color: green; white-space: pre;">...... [100%]</span>

<span style="color: green;">================= 1 passed in 0.03s =================</span>
```

</div>