Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. unit test

unit test

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++testingdebugginghelp
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L lucy 0

    I'm working with VC++6.0. I have a SDI application. I want to do unit test on some functions, but how do I run it? Say I have a function BOOL IsValid (MyType *p); And the unit test I write for it is: void TestIsValid() When I run my application in Debug mode, how can I run "TestIsValid"? Thank you very much for any help!

    W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #2

    Use the #ifdef and #endif blocks:

    BOOL IsValid (MyType *p) {
    // do something
    #ifdef _DEBUG
    TestIsValid();
    #endif
    }

    L 1 Reply Last reply
    0
    • W Waldermort

      Use the #ifdef and #endif blocks:

      BOOL IsValid (MyType *p) {
      // do something
      #ifdef _DEBUG
      TestIsValid();
      #endif
      }

      L Offline
      L Offline
      lucy 0
      wrote on last edited by
      #3

      thank you, walder. but in my "TestIsValid()", I will call "IsValid()", I'm afraid this will cause an infinite recurrent problem. the other problem is, "IsValid( )" is called quite a few times in the application, I don't want to run the unit test code all the time. Is there a way to get something like a shell window, and I just type in "TestIsValid" to get it run? Thanks!

      T W 2 Replies Last reply
      0
      • L lucy 0

        thank you, walder. but in my "TestIsValid()", I will call "IsValid()", I'm afraid this will cause an infinite recurrent problem. the other problem is, "IsValid( )" is called quite a few times in the application, I don't want to run the unit test code all the time. Is there a way to get something like a shell window, and I just type in "TestIsValid" to get it run? Thanks!

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #4

        deport your unit tests into their own project into the solution, with a reference to the sources to be tested.


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        L 1 Reply Last reply
        0
        • T toxcct

          deport your unit tests into their own project into the solution, with a reference to the sources to be tested.


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          L Offline
          L Offline
          lucy 0
          wrote on last edited by
          #5

          thank you, toxcct. Can you elaborate on how to do that?

          1 Reply Last reply
          0
          • L lucy 0

            thank you, walder. but in my "TestIsValid()", I will call "IsValid()", I'm afraid this will cause an infinite recurrent problem. the other problem is, "IsValid( )" is called quite a few times in the application, I don't want to run the unit test code all the time. Is there a way to get something like a shell window, and I just type in "TestIsValid" to get it run? Thanks!

            W Offline
            W Offline
            Waldermort
            wrote on last edited by
            #6

            Sorry, I'm having a difficult time trying to understand what you are doing? It sounds like you have a function TestIsValid() which in turn calls IsValid() which in turn calls TestIsValid(), and so on... Perhaps you could show me some code. You dont have to use the only the DEBUG definition, you can create your own

            #ifdef DEBUG
            #ifdef MYFUNC_1
            // do something
            #endif
            #endif

            T L 2 Replies Last reply
            0
            • W Waldermort

              Sorry, I'm having a difficult time trying to understand what you are doing? It sounds like you have a function TestIsValid() which in turn calls IsValid() which in turn calls TestIsValid(), and so on... Perhaps you could show me some code. You dont have to use the only the DEBUG definition, you can create your own

              #ifdef DEBUG
              #ifdef MYFUNC_1
              // do something
              #endif
              #endif

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #7

              do you know what unit tests are for ? they are written to verify that management rules are correctly implemented. for this, unit tests can be ran, but they mustn't be included in the main project as their code is only for the development purpose...


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

              Z W 2 Replies Last reply
              0
              • W Waldermort

                Sorry, I'm having a difficult time trying to understand what you are doing? It sounds like you have a function TestIsValid() which in turn calls IsValid() which in turn calls TestIsValid(), and so on... Perhaps you could show me some code. You dont have to use the only the DEBUG definition, you can create your own

                #ifdef DEBUG
                #ifdef MYFUNC_1
                // do something
                #endif
                #endif

                L Offline
                L Offline
                lucy 0
                wrote on last edited by
                #8

                here's the sample code: BOOL IsValid(MyType *p) { /* test whether MyType *p is valid. * if valid, return TRUE. * return FALSE otherwise */ ..... } void TestIsValid () { MyType a; BOOL bRet; bRet = IsValid (&a); printf ("IsValid( ) returns %d.\n", bRet); } So I don't see I can use what you recommended first as: BOOL IsValid (MyType *p) { ... #ifdef _DEBUG TestIsValid(); #endif } I hope I made myself clear this time.

                W 1 Reply Last reply
                0
                • T toxcct

                  do you know what unit tests are for ? they are written to verify that management rules are correctly implemented. for this, unit tests can be ran, but they mustn't be included in the main project as their code is only for the development purpose...


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  Z Offline
                  Z Offline
                  Zac Howland
                  wrote on last edited by
                  #9

                  toxcct wrote:

                  for this, unit tests can be ran, but they mustn't be included in the main project as their code is only for the development purpose

                  That is, of course, unless you are following one of the Agile Design models. In almost all of those paradigms, the unit test cases should accompany the source code, but won't get linked into the final application (generally, you have a separate unit-test project file that has a main.cpp that simply calls all your unit tests and reports the results).

                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                  1 Reply Last reply
                  0
                  • L lucy 0

                    here's the sample code: BOOL IsValid(MyType *p) { /* test whether MyType *p is valid. * if valid, return TRUE. * return FALSE otherwise */ ..... } void TestIsValid () { MyType a; BOOL bRet; bRet = IsValid (&a); printf ("IsValid( ) returns %d.\n", bRet); } So I don't see I can use what you recommended first as: BOOL IsValid (MyType *p) { ... #ifdef _DEBUG TestIsValid(); #endif } I hope I made myself clear this time.

                    W Offline
                    W Offline
                    Waldermort
                    wrote on last edited by
                    #10

                    So place it in the other function:

                    void TestIsValid()
                    {
                    MyType a;
                    BOOL bRet = false;
                    #ifdef _DEBUG
                    bRet = IsValid (&a);
                    printf ("IsValid( ) returns %d.\n", bRet);
                    #endif
                    }

                    1 Reply Last reply
                    0
                    • T toxcct

                      do you know what unit tests are for ? they are written to verify that management rules are correctly implemented. for this, unit tests can be ran, but they mustn't be included in the main project as their code is only for the development purpose...


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      W Offline
                      W Offline
                      Waldermort
                      wrote on last edited by
                      #11

                      To be honest I have never heard of them, but I guess they are something similar to the ASSERT() macro, only with more functionality.

                      Z 1 Reply Last reply
                      0
                      • W Waldermort

                        To be honest I have never heard of them, but I guess they are something similar to the ASSERT() macro, only with more functionality.

                        Z Offline
                        Z Offline
                        Zac Howland
                        wrote on last edited by
                        #12

                        waldermort wrote:

                        To be honest I have never heard of them

                        You have never heard of unit tests?

                        waldermort wrote:

                        similar to the ASSERT() macro, only with more functionality.

                        Not really. Unit tests isolate a module and do a thorough diagnostic on it. That is, a good unit test will test all the public methods of a module to make sure they give the desired results.

                        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                        L 1 Reply Last reply
                        0
                        • Z Zac Howland

                          waldermort wrote:

                          To be honest I have never heard of them

                          You have never heard of unit tests?

                          waldermort wrote:

                          similar to the ASSERT() macro, only with more functionality.

                          Not really. Unit tests isolate a module and do a thorough diagnostic on it. That is, a good unit test will test all the public methods of a module to make sure they give the desired results.

                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          L Offline
                          L Offline
                          led mike
                          wrote on last edited by
                          #13

                          Zac Howland wrote:

                          You have never heard of unit tests?

                          That surprises you? :cool:

                          led mike

                          Z 1 Reply Last reply
                          0
                          • L led mike

                            Zac Howland wrote:

                            You have never heard of unit tests?

                            That surprises you? :cool:

                            led mike

                            Z Offline
                            Z Offline
                            Zac Howland
                            wrote on last edited by
                            #14

                            It does surprise me a bit. That is one of the topics that should be required for any computer science/computer engineering curiculum, but I guess some places don't think you need to test your work :-P

                            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                            L 1 Reply Last reply
                            0
                            • Z Zac Howland

                              It does surprise me a bit. That is one of the topics that should be required for any computer science/computer engineering curiculum, but I guess some places don't think you need to test your work :-P

                              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                              L Offline
                              L Offline
                              led mike
                              wrote on last edited by
                              #15

                              Zac Howland wrote:

                              That is one of the topics that should be required

                              one of the "many" that aren't. :(

                              led mike

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups