-
Notifications
You must be signed in to change notification settings - Fork 5
Unit test #6
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
taipt-0504
wants to merge
1
commit into
sun7pro:master
Choose a base branch
from
taipt-0504:unit_test
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
Unit test #6
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -2,9 +2,58 @@ | |
|
|
||
| namespace Tests\C0; | ||
|
|
||
| use App\ProcessRunner; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class ProcessRunnerTest extends TestCase | ||
| { | ||
| // TODO: write 2 tests to coverage 100% code | ||
| private $processRunner; | ||
|
|
||
| public function __construct() | ||
| { | ||
| parent::__construct(); | ||
| $this->processRunner = new ProcessRunner(); | ||
| } | ||
|
|
||
| public function testRunWhenIf1TrueAndIf2True() | ||
| { | ||
| $result = $this->processRunner->run(12, 3); | ||
|
|
||
| $this->assertEquals(2, $result); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chưa đủ assert, vì không ở test case dưới https://github.com/sun7pro/phpunit-training-coverage/pull/6/files#diff-2ccad413df4fa96d3b36fd1b9389b09fR29 kết quả trả về vẫn là 2, làm sao để phân biệt? |
||
| } | ||
|
|
||
| public function testRunWhenIf1FalseAndIf2Fasle() | ||
| { | ||
| $result = $this->processRunner->run(7, 5); | ||
|
|
||
| $this->assertEquals(2, $result); | ||
| } | ||
|
|
||
| public function testWasProcess1Called() | ||
| { | ||
| $result = $this->processRunner->wasProcess1Called(); | ||
|
|
||
| $this->assertFalse($result); | ||
| } | ||
|
|
||
| public function testWasProcess2Called() | ||
| { | ||
| $result = $this->processRunner->wasProcess2Called(); | ||
|
|
||
| $this->assertFalse($result); | ||
| } | ||
|
|
||
| public function testWasProcess3Called() | ||
| { | ||
| $result = $this->processRunner->wasProcess3Called(); | ||
|
|
||
| $this->assertFalse($result); | ||
| } | ||
|
|
||
| public function testWasProcess4Called() | ||
| { | ||
| $result = $this->processRunner->wasProcess4Called(); | ||
|
|
||
| $this->assertFalse($result); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Nên coi class là Unit cần test, tức class là SUT (System Under Test). Vì thế test case sẽ là test hành vi,không nên quá quan tâm đến tên method là gì, bên trong nó implement như thế nào, khi đó có thể thay đổi cách implement nhưng hành vi không thay đổi nên không phải update lại unit test.
Tên method unit test không cần phải ngắn gọn, nên viết theo snake_case để dễ đọc hơn (có thể config phpcs để bỏ qua naming convention với các method test, xem phpcs.xml)
Có 1 cách đặt tên như thế này:
Test It <expected result> When <input are ...>=> Test It called process1 and process3 When input1 is 12 and input2 can be whatever
Tên test case cho thấy rõ ràng expect là gì.