fga-eps-mds / 2020.2-Anunbis

Aplicação que auxilia na avaliação de professores e disciplinas da UnB. Anunbis: https://anunbis-frontend.herokuapp.com/. Anunbis-API: http://anunbis-backend.herokuapp.com/
https://fga-eps-mds.github.io/2020.2-Anunbis/
GNU General Public License v3.0
5 stars 11 forks source link

Testes de Integração do Backend #152

Closed thiagohdaqw closed 3 years ago

thiagohdaqw commented 3 years ago

Descrição:

Conforme o projeto avança, a necessidade de testes se torna cada vez mais claro, pois a cada modificação se gasta grande tempo testando se nada quebrou. Assim, adicionar os testes de integração de cada rota é imprescindível.

Tarefas:

Critérios de aceitação:

thiagohdaqw commented 3 years ago

Como rodar os testes

Make

make test

Problema de permissao no linux

Caso tenha problema de permissao para acessar os arquivos .sh, utilize o comando chmod +x scripts/NOME_DO_SCRIPT.sh

Interpretações dos testes

Apos rodar os testes, mostrado acima, o coverage gerará diversos relatorios importantes.

Primeiramente será executado todos os testes e apos é apresentado o seus resultados, ou seja, se o teste funcionou ou não, como mostra a figura abaixo. image Na figura, foi testado 1 método e o resultado foi de sucesso.

Se todos os testes forem executados com sucesso, após é mostrado um relatorio com a cobertura do codigo. image Na figura, na coluna Cover, é mostrado a porcentagem do codigo que foi testado.

Outra coisa legal, é que o Coverage gera um relatorio em html, localizado em htmlcov/index.html que voce pode abrir no navegador e ver a cobertura com mais detalhes.

image image

thiagohdaqw commented 3 years ago

Comandos importantes do Coverage

Comando Descrição
coverage run --source=app -m unittest discover -s tests/ -v Executa todos os testes na pasta /tests
coverage report Mostra relatorio de cobertura
coverage html Gera o arquivo html
thiagohdaqw commented 3 years ago

IPDB

IPDB é uma ferramenta para debug muito util, pois ela permite que consigamos parar a execução do programa para visualizar valores de variaveis.

Comandos

Comando Descrição
import ipdb; ipdb.set_trace() Codigo a ser colocado no lugar do codigo que quiser debugar
ll Imprime todo o metodo
c Sai do IPDB e continua a execução

Como usar

Para usar o IPDB, apenas coloca a seguinte linha de codigo no local onde voce quer debugar. import ipdb; ipdb.set_trace()

Exemplo

Coloquei import ipdb; ipdb.set_trace() dentro de um codigo aleatorio image

Agora quando eu executar o codigo, consigo ter acesso àquelas variaveis. image

Assim, pelo terminal, consigo ter acesso à variavel image

thiagohdaqw commented 3 years ago

IPDB - Docker

Para que o IPDB funcione no docker, é preciso iniciar o docker com o seguinte comando:

docker-compose run --service-ports flask

thiagohdaqw commented 3 years ago

Para mais informações, acesse a documentação do Unittest

thiagohdaqw commented 3 years ago

Métodos úteis do Unittest

Method

Checks that

assertEqual(a, b)

a == b

assertNotEqual(a, b)

a != b

assertTrue(x)

bool(x) is True

assertFalse(x)

bool(x) is False

assertIs(a, b)

a is b

assertIsNot(a, b)

a is not b

assertIsNone(x)

x is None

assertIsNotNone(x)

x is not None

assertIn(a, b)

a in b

assertNotIn(a, b)

a not in b

assertIsInstance(a, b)

isinstance(a, b)

assertNotIsInstance(a, b)

not isinstance(a, b)

thiagohdaqw commented 3 years ago

Atributos do Response

image

accept_ranges The Accept-Ranges header.
age The Age response-header field conveys the sender’s estimate of the amount of time since the response (or its revalidation) was generated at the origin server.
allow The Allow entity-header field lists the set of methods supported by the resource identified by the Request-URI.
autocorrect_location_header
automatically_set_content_length
cache_control The Cache-Control general-header field is used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain.
charset
content_encoding The Content-Encoding entity-header field is used as a modifier to the media-type.
content_language The Content-Language entity-header field describes the natural language(s) of the intended audience for the enclosed entity.
content_length The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.
content_location The Content-Location entity-header field MAY be used to supply the resource location for the entity enclosed in the message when that entity is accessible from a location separate from the requested resource’s URI.
content_md5 The Content-MD5 entity-header field, as defined in RFC 1864, is an MD5 digest of the entity-body for the purpose of providing an end-to-end message integrity check (MIC) of the entity-body.
content_range The Content-Range header as ContentRange object.
content_type The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the case of the HEAD method, the media type that would have been sent had the request been a GET.
data A descriptor that calls get_data() and set_data().
date The Date general-header field represents the date and time at which the message was originated, having the same semantics as orig-date in RFC 822.
default_mimetype
default_status
expires The Expires entity-header field gives the date/time after which the response is considered stale.
implicit_sequence_conversion
is_sequence If the iterator is buffered, this property will be True.
is_streamed If the response is streamed (the response is not an iterable with a length information) this property is True.
last_modified The Last-Modified entity-header field indicates the date and time at which the origin server believes the variant was last modified.
location The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource.
mimetype The mimetype (content type without charset etc.)
mimetype_params The mimetype parameters as dict.
retry_after The Retry-After response-header field can be used with a 503 (Service Unavailable) response to indicate how long the service is expected to be unavailable to the requesting client.
status The HTTP Status code
status_code The HTTP Status code as number
stream The response iterable as write-only stream.
vary The Vary field value indicates the set of request-header fields that fully determines, while the response is fresh, whether a cache is permitted to use the response to reply to a subsequent request without revalidation.
www_authenticate The WWW-Authenticate header in a parsed form.