8
const
6y

Do you test your codes while coding or after completion?

I test while coding.
PS:- I'm web developer.

Comments
  • 6
    I tend to write tests before any code.
    That way i can drive out a nice API right off the bat.
  • 0
    Before coding. TDD and all that stuff.
  • 1
    @lotd Yup indeed. It is called TDD (Test Driven Development). You first write tests and write code based on those tests. This makes sure that you only write code that you need and that is works as intended. Well.. If you write your tests correctly of course :p But that is one of the many approaches you can do. As an iOS developer I test during development and again after I finished a feature ;)
  • 0
    Usually, I design top-down, but code and test bottom-up. Sometimes, tests are too cumbersome when I don't even have the printf debugger. Then I replace tests by reviews and test after completion.

    Actually, I mostly even debug by reviews.
  • 0
    i test before commit and after saves
  • 0
    @lotd but what are you going to test if you still have no code?

    Excuse my noobness in TDD but I still don't understand it :/
  • 2
    @gitpush you have a general idea of what you wanna build.

    So you write tests for the code, before you have it.
    - make a nice & fluent API, to interact with.

    Then you run the test, and let the fails guide you .. :)

    Example Unit test.
    ```
    $transaction = $user->cart->add($product)->checkout ();

    Payment::assertCharged($product->cost, $user);

    $this->assertEmpty($user->cart);

    $this->assertDatabaseHas('purchases', [
    'user_id' => $user->id,
    'product_id' => $product->id,
    'transaction_id' => $transaction->id,
    'created_at' => now(),
    ]);
    ```
  • 0
    @lotd thanks man, but looking at your code this means I need to have a user model, and a cart model, and an item model to add to cart, how do I do this if I have not yet written them
  • 1
    @gitpush well you don't write all the tests at once... Let.s say u want to make a simple api with one dto and crud operations to a db. So you create a test that gets your object init and check it.s props. Then you write your class so the test will pass then you want a DB connection. You will write a test that creates the DB connection and checks for error==null then you write your DB connector class and so on so forth
  • 0
    @zer02 still not clear :/ any tutorials you know for a newbie?
  • 0
    Write tests before Implementation.
  • 0
  • 1
    @gitpush Yeah, that's the point.
    You implement whats obvious for the test to pass.

    Then you run the test, if it fails, you solve the reason it failed and try again until it's working.. :)
  • 0
    @Brosyl finally I understand lol thanks man, though this is writing tests after code, which is used at what scenarios?
    @lotd thanks man 😀
  • 0
    @Brosyl so just like everything else, it's used when it should and not like for example, clean coding style it must always be used
Add Comment