-
Notifications
You must be signed in to change notification settings - Fork 0
hw03 test fw #3
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
Open
1121977
wants to merge
7
commits into
master
Choose a base branch
from
hw03-testFW
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
hw03 test fw #3
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f10fdf4
Deleted .idea and .gradle from index
1121977 de77288
v.10 Test Framework was started.
1121977 063f8cf
v11. Ready to check.
1121977 2589483
v12. Before refactoring.
1121977 958b9fb
v13. Refactored.
1121977 809ea05
Delete A.java
1121977 af8bb30
README update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,14 @@ | ||
| # Примеры для курса "Разработчик Java" в OTUS | ||
| Написать свой тестовый фреймворк. | ||
|
|
||
| Группа 2020-09 | ||
| Поддержать свои аннотации @Test, @Before, @After. | ||
|
|
||
| ### Преподаватели | ||
| Сергей Петрелевич<br> | ||
| Стрекалов Павел<br> | ||
| Александр Оруджев<br> | ||
| Вячеслав Лапин<br> | ||
| Виталий Куценко<br> | ||
| Дмитрий Коган | ||
| Запускать вызовом статического метода с именем класса с тестами. | ||
|
|
||
| Студент: | ||
| Kashapov Renat (Кашапов Ренат)<br> | ||
| renat.kashapov@gmail.com | ||
| Т.е. надо сделать: | ||
|
|
||
| создать три аннотации - @Test, @Before, @After. | ||
| Создать класс-тест, в котором будут методы, отмеченные аннотациями. | ||
| Создать "запускалку теста". На вход она должна получать имя класса с тестами, в котором следует найти и запустить методы отмеченные аннотациями и пункта 1. | ||
| Алгоритм запуска должен быть следующий:: метод(ы) Before текущий метод Test метод(ы) After для каждой такой "тройки" надо создать СВОЙ объект класса-теста. | ||
| Исключение в одном тесте не должно прерывать весь процесс тестирования. | ||
| На основании возникших во время тестирования исключений вывести статистику выполнения тестов (сколько прошло успешно, сколько упало, сколько было всего) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| plugins { | ||
| id 'java' | ||
| id 'application' | ||
| } | ||
|
|
||
| group 'ru.otus' | ||
| version '1.0-SNAPSHOT' | ||
|
|
||
| mainClassName = 'ru.otus.TestFW' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package ru.otus; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface After { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package ru.otus; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface Before { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package ru.otus; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface Test { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package ru.otus; | ||
|
|
||
| import java.lang.annotation.Annotation; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.*; | ||
|
|
||
| public class TestFW { | ||
| private static short failedTest; | ||
| public static void main(String ... args){ | ||
| System.out.println("Hello, Test Framework"); | ||
| testStarter(TestedClass.class); | ||
| } | ||
| static void testStarter(Class<?> clazz) { | ||
| Method [] methods = clazz.getDeclaredMethods(); | ||
| Map<Class<? extends Annotation>, Set<Method>> annotationSetMethod = new HashMap<>(); | ||
| annotationSetMethod.put(Before.class, new HashSet<>()); | ||
| annotationSetMethod.put(Test.class, new HashSet<>()); | ||
| annotationSetMethod.put(After.class, new HashSet<>()); | ||
| for(Method method:methods){ | ||
| for(Annotation annotation:method.getDeclaredAnnotations()){ | ||
| Set<Method> methodSet = annotationSetMethod.get(annotation.annotationType()); | ||
| if(methodSet!=null){ | ||
| methodSet.add(method); | ||
| } | ||
| } | ||
| } | ||
| annotationSetMethod.get(Test.class).forEach(methodTest -> { | ||
| Object testedObject; | ||
| try { | ||
| testedObject = clazz.getConstructor().newInstance(); | ||
| if (testedObject == null) { | ||
| throw new NullPointerException(); | ||
| } else { | ||
| try { | ||
| methodsExecution(annotationSetMethod.get(Before.class), testedObject); | ||
| methodTest.invoke(testedObject); | ||
| methodsExecution(annotationSetMethod.get(After.class), testedObject); | ||
| } catch (Exception e) { | ||
| failedTest++; | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| }); | ||
| System.out.println("Failed tests: " + failedTest); | ||
| System.out.println("Passed tests: " + (methods.length-failedTest)); | ||
| } | ||
|
|
||
| private static void methodsExecution(Set<Method> methodSet, Object testedObject){ | ||
| methodSet.forEach(method -> { | ||
| try { | ||
| method.invoke(testedObject); | ||
| } | ||
| catch (IllegalAccessException | InvocationTargetException e){ | ||
| e.printStackTrace(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package ru.otus; | ||
|
|
||
| public class TestedClass { | ||
|
|
||
| public TestedClass(){} | ||
|
|
||
| @After | ||
| public void methodAfter1(){System.out.println("I'm methodAfter1 (this.hashCode(): " + hashCode() + ")");} | ||
|
|
||
| @After | ||
| public void methodAfter2(){System.out.println("I'm methodAfter2 (this.hashCode(): " + hashCode() + ")");} | ||
|
|
||
| @Before | ||
| public void methodBefore1(){ | ||
| System.out.println("I'm methodBefore1 (this.hashCode(): " + hashCode() + ")"); | ||
| } | ||
|
|
||
| @Before | ||
| public void methodBefore2(){ | ||
| System.out.println("I'm methodBefore2 (this.hashCode(): " + hashCode() + ")"); | ||
| } | ||
|
|
||
| @Test | ||
| public void methodTest1(){ | ||
| System.out.println("I'm methodTest1 (this.hashCode(): " + hashCode() + ")"); | ||
| throw new ArithmeticException(); | ||
| } | ||
|
|
||
| @Test | ||
| public void methodTest2() {System.out.println("I'm methodTest2 (this.hachCode(): " + hashCode() + ")");} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| rootProject.name = 'otus_hw' | ||
|
|
||
| include 'hw03-testFW' |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
если в before или after будет ошибка, то тест отметится как успешный?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, именно так и произойдет. Тут я не совсем понимаю целевого назначения тестирования относительно Before и After. Методы, помеченные ими, также являются частью тестов? Исходил из того, что бизнес ценностью являются методы, помеченные Test, ради них всё и затевается.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
формально тест состоит из трех этапов: подготовка данных - выполнение - завершение.
Я бы считал тест пройденным, если успешно выполнены все три этапа.
Но это не принципиально для ДЗ.